Re: [swift-evolution] Pattern matching with Arrays

2017-01-17 Thread Joseph Newton via swift-evolution
+1

I've had many a use case for this and would benefit from it being added. 

> On Jan 17, 2017, at 10:39, Mathew Sanders via swift-evolution 
>  wrote:
> 
> Great to hear that more pattern matching is likely in the Swift's future, and 
> whatever form it takes I'm looking forward to it :)
> 
>> Do you think it would make sense to make this an official part of 
>> swift-evolution?  Perhaps an analog to the “commonly rejected” list?
>> https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md
> 
> 
> +1 on that, I'd love to have a peek ahead to see the direction swift might be 
> taking in the long-term... and I actually looked for something like this 
> before I mailed the list.
> 
>> On 17 January 2017 at 01:21, Chris Lattner  wrote:
>> 
>>> On Jan 16, 2017, at 9:53 PM, Jay Abbott via swift-evolution 
>>>  wrote:
>>> 
>>> Regarding the "some-day" list (or "deferred" proposals): It has been 
>>> discussed many times before, and Chris also agreed that it would be 
>>> useful[1], but nobody seems to have made it happen. So I have created a 
>>> trello board:
>>> https://trello.com/b/dkVHvhGn/swift-deferred-proposals
>>> 
>>> Hopefully we can maintain it collectively, and it should be a handy quick 
>>> reference to previous discussions about specific subjects. I've added a few 
>>> lists/cards to get started, including a link to this discussion about 
>>> pattern matching. There's a README on the board that contains a link to 
>>> gain add/edit access to the board.
>> 
>> Hi Jay,
>> 
>> Do you think it would make sense to make this an official part of 
>> swift-evolution?  Perhaps an analog to the “commonly rejected” list?
>> https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md
>> 
>> -Chris
> 
> ___
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] Pattern matching with Arrays

2017-01-17 Thread Mathew Sanders via swift-evolution
Great to hear that more pattern matching is likely in the Swift's future,
and whatever form it takes I'm looking forward to it :)

Do you think it would make sense to make this an official part of
> swift-evolution?  Perhaps an analog to the “commonly rejected” list?
> https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md


+1 on that, I'd love to have a peek ahead to see the direction swift might
be taking in the long-term... and I actually looked for something like this
before I mailed the list.

On 17 January 2017 at 01:21, Chris Lattner  wrote:

>
> On Jan 16, 2017, at 9:53 PM, Jay Abbott via swift-evolution <
> swift-evolution@swift.org> wrote:
>
> Regarding the "some-day" list (or "deferred" proposals): It has been
> discussed many times before, and Chris also agreed that it would be
> useful[1], but nobody seems to have made it happen. So I have created a
> trello board:
> https://trello.com/b/dkVHvhGn/swift-deferred-proposals
>
> Hopefully we can maintain it collectively, and it should be a handy quick
> reference to previous discussions about specific subjects. I've added a few
> lists/cards to get started, including a link to this discussion about
> pattern matching. There's a README on the board that contains a link to
> gain add/edit access to the board.
>
>
> Hi Jay,
>
> Do you think it would make sense to make this an official part of
> swift-evolution?  Perhaps an analog to the “commonly rejected” list?
> https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md
>
> -Chris
>
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] Pattern matching with Arrays

2017-01-16 Thread Chris Lattner via swift-evolution

> On Jan 16, 2017, at 9:53 PM, Jay Abbott via swift-evolution 
>  wrote:
> 
> Regarding the "some-day" list (or "deferred" proposals): It has been 
> discussed many times before, and Chris also agreed that it would be 
> useful[1], but nobody seems to have made it happen. So I have created a 
> trello board:
> https://trello.com/b/dkVHvhGn/swift-deferred-proposals 
> 
> 
> Hopefully we can maintain it collectively, and it should be a handy quick 
> reference to previous discussions about specific subjects. I've added a few 
> lists/cards to get started, including a link to this discussion about pattern 
> matching. There's a README on the board that contains a link to gain add/edit 
> access to the board.

Hi Jay,

Do you think it would make sense to make this an official part of 
swift-evolution?  Perhaps an analog to the “commonly rejected” list?
https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md 


-Chris___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] Pattern matching with Arrays

2017-01-15 Thread Jacob Bandes-Storch via swift-evolution
On Tue, Jan 3, 2017 at 10:10 AM, Joe Groff via swift-evolution <
swift-evolution@swift.org> wrote:

>
> On Dec 22, 2016, at 7:43 PM, Robert Widmann 
> wrote:
>
> Do you think there’s room for a more general Pattern Synonyms-like
>  feature that
> could extend this to things that look tuple-y?  We had a short conversation
> on Twitter 'round about the release of Swift 1.2 about Swiftz’s HList
>  
> implementation
> and my desire to be able to destructure them into tuples for native pattern
> matching.
>
>
> My personal favorite design for user-extensible pattern syntax is F#'s
> "active pattern" feature, which lets you declare a set of mutual exclusive,
> total or partial conditions along with a function body that computes which
> condition holds. For the specific case of container patterns, though, I
> feel like it's worth keeping a close association with Collection semantics,
> so that:
>
> - [] matches when the incoming collection's startIndex == endIndex,
> - [, ] fails if startIndex == endIndex, or else
> matches collection[startIndex] against  and recursively matches
> collection[startIndex.advancedBy(1).. - [...] matches the remaining collection against 
>
> -Joe
>

This is really cool. I've never used F#, so thanks for pointing this out.
https://docs.microsoft.com/en-us/dotnet/articles/fsharp/language-reference/
active-patterns  Extra points for the name (| "banana clips" |).

I've had something like this on my backburner-wishlist for a while.
Customizable patterns/bindings could be very powerful.

One use case I had in mind: writing parsers for binary formats such as
msgpack . For instance, msgpack encodes a
uint16 as 0xcd + high byte + low byte. If the 0xcd could be given as a
parameter to the "active pattern", you could imagine writing something like

match msgpackStream {  // a raw byte stream

// Scan 3 bytes, the first of which is equal to 0xcd.
// Theoretical syntax allowing a custom stream-scanner object to fill in
the two variable bindings
// by reading bytes from the stream. If the first byte were not 0xcd, the
match would fail.
case [0xcd, let hi, let lo]:
return UInt16(hi) << 8 | UInt16(lo)
...
}
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] Pattern matching with Arrays

2017-01-03 Thread Joe Groff via swift-evolution

> On Dec 22, 2016, at 7:43 PM, Robert Widmann  wrote:
> 
> Do you think there’s room for a more general Pattern Synonyms-like 
>  feature that could 
> extend this to things that look tuple-y?  We had a short conversation on 
> Twitter 'round about the release of Swift 1.2 about Swiftz’s HList 
>  
> implementation and my desire to be able to destructure them into tuples for 
> native pattern matching.

My personal favorite design for user-extensible pattern syntax is F#'s "active 
pattern" feature, which lets you declare a set of mutual exclusive, total or 
partial conditions along with a function body that computes which condition 
holds. For the specific case of container patterns, though, I feel like it's 
worth keeping a close association with Collection semantics, so that:

- [] matches when the incoming collection's startIndex == endIndex,
- [, ] fails if startIndex == endIndex, or else matches 
collection[startIndex] against  and recursively matches 
collection[startIndex.advancedBy(1)..

Re: [swift-evolution] Pattern matching with Arrays

2016-12-22 Thread Georgios Moschovitis via swift-evolution
> Yeah, this is something I've wanted to add for a while, along with pattern 
> matching for dictionaries, which together with array matching I think would 
> make a lot of JSON/plist wrangling much more pleasant.

+1
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] Pattern matching with Arrays

2016-12-22 Thread David Sweeris via swift-evolution

> On Dec 17, 2016, at 9:43 PM, Mathew Sanders via swift-evolution 
>  wrote:
> 
> I've just joined the list (hi!) so not sure if this has been discussed in the 
> past.
> 
> Curious to hear if a future version of Swift might ever include some sort of 
> pattern-matching extended to Arrays. Maybe something like the following:
> 
> let numbers: [Int]
> 
> switch numbers {
> case []:
> // to match an empty array
> 
> case [1, 2, 3]:
> // to match array with specific values
> 
> case [_, _, _]:
> // to match array with 3 Ints
> 
> case let [first, last]:
> // match a 2 element array and bind first and last elements
> 
> case let [first, next...]:
> // to match array with at least one element
> // bind first element, and bind other elements (could be empty)
> // first: Int, next: [Int]
> }
> 
> I love the pattern matching on tuples, and would love to see if extend to 
> Arrays as well, but not sure if it fits with future goals for the language.

+1

Also, +1 for Erica’s idea of a “some day” list

- Dave Sweeris___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] Pattern matching with Arrays

2016-12-22 Thread Erica Sadun via swift-evolution
On Dec 22, 2016, at 8:43 PM, Robert Widmann via swift-evolution 
 wrote:
> 
> Do you think there’s room for a more general Pattern Synonyms-like 
>  feature that could 
> extend this to things that look tuple-y?  We had a short conversation on 
> Twitter 'round about the release of Swift 1.2 about Swiftz’s HList 
>  
> implementation and my desire to be able to destructure them into tuples for 
> native pattern matching.
> 
> ~Robert Widmann
> 
>> On Dec 22, 2016, at 9:46 AM, Joe Groff via swift-evolution 
>> > wrote:
>> 
>>> 
>>> On Dec 17, 2016, at 9:43 PM, Mathew Sanders via swift-evolution 
>>> > wrote:
>>> 
>>> I've just joined the list (hi!) so not sure if this has been discussed in 
>>> the past.
>>> 
>>> Curious to hear if a future version of Swift might ever include some sort 
>>> of pattern-matching extended to Arrays. Maybe something like the following:
>>> 
>>> let numbers: [Int]
>>> 
>>> switch numbers {
>>> case []:
>>>// to match an empty array
>>> 
>>> case [1, 2, 3]:
>>>// to match array with specific values
>>> 
>>> case [_, _, _]:
>>>// to match array with 3 Ints
>>> 
>>> case let [first, last]:
>>>// match a 2 element array and bind first and last elements
>>> 
>>> case let [first, next...]:
>>>// to match array with at least one element
>>>// bind first element, and bind other elements (could be empty)
>>>// first: Int, next: [Int]
>>> }
>>> 
>>> I love the pattern matching on tuples, and would love to see if extend to 
>>> Arrays as well, but not sure if it fits with future goals for the language.
>> 
>> Yeah, this is something I've wanted to add for a while, along with pattern 
>> matching for dictionaries, which together with array matching I think would 
>> make a lot of JSON/plist wrangling much more pleasant. Since it's an 
>> additive feature, it's taken a backseat to more pressing source- and 
>> ABI-breaking changes we've wanted to get out of the way first.

I'd be excited to see a feature like this for arrays, dictionaries, and 
array-like items such as strings. I know that this cannot be addressed for a 
while, but I'd love to see the idea formalized a bit and placed on a list 
somewhere so it doesn't just disappear into the aether. 

It's a pity we don't have a formal registry for "Some Day" items like this that 
get mentioned on-list, but don't properly fit into Jira or the SE pull-request 
queue.

-- E

___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] Pattern matching with Arrays

2016-12-22 Thread Robert Widmann via swift-evolution
Do you think there’s room for a more general Pattern Synonyms-like 
 feature that could 
extend this to things that look tuple-y?  We had a short conversation on 
Twitter 'round about the release of Swift 1.2 about Swiftz’s HList 
 
implementation and my desire to be able to destructure them into tuples for 
native pattern matching.

~Robert Widmann

> On Dec 22, 2016, at 9:46 AM, Joe Groff via swift-evolution 
>  wrote:
> 
>> 
>> On Dec 17, 2016, at 9:43 PM, Mathew Sanders via swift-evolution 
>>  wrote:
>> 
>> I've just joined the list (hi!) so not sure if this has been discussed in 
>> the past.
>> 
>> Curious to hear if a future version of Swift might ever include some sort of 
>> pattern-matching extended to Arrays. Maybe something like the following:
>> 
>> let numbers: [Int]
>> 
>> switch numbers {
>> case []:
>>// to match an empty array
>> 
>> case [1, 2, 3]:
>>// to match array with specific values
>> 
>> case [_, _, _]:
>>// to match array with 3 Ints
>> 
>> case let [first, last]:
>>// match a 2 element array and bind first and last elements
>> 
>> case let [first, next...]:
>>// to match array with at least one element
>>// bind first element, and bind other elements (could be empty)
>>// first: Int, next: [Int]
>> }
>> 
>> I love the pattern matching on tuples, and would love to see if extend to 
>> Arrays as well, but not sure if it fits with future goals for the language.
> 
> Yeah, this is something I've wanted to add for a while, along with pattern 
> matching for dictionaries, which together with array matching I think would 
> make a lot of JSON/plist wrangling much more pleasant. Since it's an additive 
> feature, it's taken a backseat to more pressing source- and ABI-breaking 
> changes we've wanted to get out of the way first.
> 
> -Joe
> ___
> swift-evolution mailing list
> swift-evolution@swift.org 
> https://lists.swift.org/mailman/listinfo/swift-evolution 
> 
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] Pattern matching with Arrays

2016-12-22 Thread Joe Groff via swift-evolution

> On Dec 17, 2016, at 9:43 PM, Mathew Sanders via swift-evolution 
>  wrote:
> 
> I've just joined the list (hi!) so not sure if this has been discussed in the 
> past.
> 
> Curious to hear if a future version of Swift might ever include some sort of 
> pattern-matching extended to Arrays. Maybe something like the following:
> 
> let numbers: [Int]
> 
> switch numbers {
> case []:
> // to match an empty array
> 
> case [1, 2, 3]:
> // to match array with specific values
> 
> case [_, _, _]:
> // to match array with 3 Ints
> 
> case let [first, last]:
> // match a 2 element array and bind first and last elements
> 
> case let [first, next...]:
> // to match array with at least one element
> // bind first element, and bind other elements (could be empty)
> // first: Int, next: [Int]
> }
> 
> I love the pattern matching on tuples, and would love to see if extend to 
> Arrays as well, but not sure if it fits with future goals for the language.

Yeah, this is something I've wanted to add for a while, along with pattern 
matching for dictionaries, which together with array matching I think would 
make a lot of JSON/plist wrangling much more pleasant. Since it's an additive 
feature, it's taken a backseat to more pressing source- and ABI-breaking 
changes we've wanted to get out of the way first.

-Joe
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] Pattern matching with Arrays

2016-12-19 Thread Anton Zhilin via swift-evolution
2016-12-19 3:09 GMT+03:00 Lucas Neiva via swift-evolution <
swift-evolution@swift.org>:

> case let [first, next...]:
> > case let [first, last]:
>
> The binding should be more like "[let first, let last]" though, to be more
> like the tuple matching. For the above also: "case [let head, let tail...]".
>
Actually, with tuples, case let (first, last) is equivalent to case (let
first, let last), so the “compact” form should be ok with Array pattern
matching.
​
___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


Re: [swift-evolution] Pattern matching with Arrays

2016-12-18 Thread Lucas Neiva via swift-evolution
As a fellow functional programming aficionado, I'd like this very much, too. 
From lurking here, I think something like this is of low priority at the moment 
since it is and additive feature.

Especially head/tail matching looks very interesting. I also like the syntax 
you suggest here:
> case let [first, next...]:


> case let [first, last]:

The binding should be more like "[let first, let last]" though, to be more like 
the tuple matching. For the above also: "case [let head, let tail...]".


> On 18 Dec 2016, at 06:43, Mathew Sanders via swift-evolution 
>  wrote:
> 
> I've just joined the list (hi!) so not sure if this has been discussed in the 
> past.
> 
> Curious to hear if a future version of Swift might ever include some sort of 
> pattern-matching extended to Arrays. Maybe something like the following:
> 
> let numbers: [Int]
> 
> switch numbers {
> case []:
> // to match an empty array
> 
> case [1, 2, 3]:
> // to match array with specific values
> 
> case [_, _, _]:
> // to match array with 3 Ints
> 
> case let [first, last]:
> // match a 2 element array and bind first and last elements
> 
> case let [first, next...]:
> // to match array with at least one element
> // bind first element, and bind other elements (could be empty)
> // first: Int, next: [Int]
> }
> 
> I love the pattern matching on tuples, and would love to see if extend to 
> Arrays as well, but not sure if it fits with future goals for the language.
> 
> 
> ___
> swift-evolution mailing list
> swift-evolution@swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

___
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution