Pattern matching with Arrays

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 Like

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 <swift-evolution@swift.org> 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

1 Like

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

···

On Dec 17, 2016, at 9:43 PM, Mathew Sanders via swift-evolution <swift-evolution@swift.org> 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 Like

+1

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

- Dave Sweeris

···

On Dec 17, 2016, at 9:43 PM, Mathew Sanders via swift-evolution <swift-evolution@swift.org> 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.

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.

···

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...]".

Do you think there’s room for a more general Pattern Synonyms-like <https://ghc.haskell.org/trac/ghc/wiki/PatternSynonyms&gt; 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 <https://github.com/typelift/Swiftz/blob/master/Sources/HList.swift#L185&gt; 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 <swift-evolution@swift.org> wrote:

On Dec 17, 2016, at 9:43 PM, Mathew Sanders via swift-evolution <swift-evolution@swift.org> 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 <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/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

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:

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.

[1]
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160926/027353.html

···

On Fri, 23 Dec 2016 at 04:38 David Sweeris via swift-evolution < swift-evolution@swift.org> wrote:

On Dec 17, 2016, at 9:43 PM, Mathew Sanders via swift-evolution < > swift-evolution@swift.org> 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

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

···

On Dec 22, 2016, at 8:43 PM, Robert Widmann via swift-evolution <swift-evolution@swift.org> wrote:

Do you think there’s room for a more general Pattern Synonyms-like <https://ghc.haskell.org/trac/ghc/wiki/PatternSynonyms&gt; 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 <https://github.com/typelift/Swiftz/blob/master/Sources/HList.swift#L185&gt; 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 <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

On Dec 17, 2016, at 9:43 PM, Mathew Sanders via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> 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.

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,
- [<pattern>, <pattern-list>] fails if startIndex == endIndex, or else matches collection[startIndex] against <pattern> and recursively matches collection[startIndex.advancedBy(1)..<endIndex] against [<pattern-list>]
- [<pattern>...] matches the remaining collection against <pattern>

-Joe

···

On Dec 22, 2016, at 7:43 PM, Robert Widmann <devteam.codafi@gmail.com> wrote:

Do you think there’s room for a more general Pattern Synonyms-like <https://ghc.haskell.org/trac/ghc/wiki/PatternSynonyms&gt; 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 <https://github.com/typelift/Swiftz/blob/master/Sources/HList.swift#L185&gt; implementation and my desire to be able to destructure them into tuples for native pattern matching.

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?

-Chris

···

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:
Trello

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.

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 <sabre@nondot.org> 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:
Trello

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

This is really cool. I've never used F#, so thanks for pointing this out.

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 <http://msgpack.org/index.html&gt;\. 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)
...
}

···

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 <devteam.codafi@gmail.com> > wrote:

Do you think there’s room for a more general Pattern Synonyms-like
<https://ghc.haskell.org/trac/ghc/wiki/PatternSynonyms&gt; 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
<https://github.com/typelift/Swiftz/blob/master/Sources/HList.swift#L185&gt; 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,
- [<pattern>, <pattern-list>] fails if startIndex == endIndex, or else
matches collection[startIndex] against <pattern> and recursively matches
collection[startIndex.advancedBy(1)..<endIndex] against [<pattern-list>]
- [<pattern>...] matches the remaining collection against <pattern>

-Joe

+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 <swift-evolution@swift.org> 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 <sabre@nondot.org> 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:
Trello

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