# Pitch: Support for map and flatMap with smart key paths

**URL:** <https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073>\
**Category:** Pitches\
**Created:** [June 7, 2017, 5:35pm UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073 "2017-06-07T17:35:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sharplet](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sharplet/32/6184_2.png) [@sharplet](https://forums.swift.org/u/sharplet)\
**Post date:** [June 7, 2017, 5:35pm UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/1 "2017-06-07T17:35:49Z")

</div>

The new smart key path feature is really lovely, and feels like a great addition to Swift.

It seems like it might be straightforward to add overloads of `map` and `flatMap` to the standard library to make use of the new functionality:

&nbsp;&nbsp;let managers = flatOrganisation.managers  
&nbsp;&nbsp;let allEmployees = Set(managers.flatMap(\.directReports))  
&nbsp;&nbsp;let employeeNames = Set(allEmployees.map(\.name))

This feels like a really natural way of working with key paths in a functional style. It makes a lot of sense for collections, and possibly for Optional too (although as far as I can see optional chaining is more or less equivalent, and with more compact syntax).

I’m hoping that this might be low-hanging fruit that could be considered for the Swift 4 release. I’d be happy to have a go at writing a proposal if there’s interest!

–Adam

---

<div class="post-metadata">

**Author:** ![Matt\_Diephouse1](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/matt_diephouse1/32/498_2.png) [@Matt\_Diephouse1](https://forums.swift.org/u/Matt_Diephouse1)\
**Post date:** [June 7, 2017, 6:16pm UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/2 "2017-06-07T18:16:41Z")

</div>

💯

> **···**
>
> > On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > The new smart key path feature is really lovely, and feels like a great addition to Swift.
> > 
> > It seems like it might be straightforward to add overloads of `map` and `flatMap` to the standard library to make use of the new functionality:
> > 
> > &nbsp;&nbsp;let managers = flatOrganisation.managers  
> > &nbsp;&nbsp;let allEmployees = Set(managers.flatMap(\.directReports))  
> > &nbsp;&nbsp;let employeeNames = Set(allEmployees.map(\.name))
> > 
> > This feels like a really natural way of working with key paths in a functional style. It makes a lot of sense for collections, and possibly for Optional too (although as far as I can see optional chaining is more or less equivalent, and with more compact syntax).
> > 
> > I’m hoping that this might be low-hanging fruit that could be considered for the Swift 4 release. I’d be happy to have a go at writing a proposal if there’s interest!
> > 
> > –Adam
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![mdiep](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mdiep/32/500_2.png) [@mdiep](https://forums.swift.org/u/mdiep)\
**Post date:** [June 7, 2017, 6:18pm UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/3 "2017-06-07T18:18:34Z")

</div>

💯

> **···**
>
> > On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > The new smart key path feature is really lovely, and feels like a great addition to Swift.
> > 
> > It seems like it might be straightforward to add overloads of `map` and `flatMap` to the standard library to make use of the new functionality:
> > 
> > &nbsp;&nbsp;let managers = flatOrganisation.managers  
> > &nbsp;&nbsp;let allEmployees = Set(managers.flatMap(\.directReports))  
> > &nbsp;&nbsp;let employeeNames = Set(allEmployees.map(\.name))
> > 
> > This feels like a really natural way of working with key paths in a functional style. It makes a lot of sense for collections, and possibly for Optional too (although as far as I can see optional chaining is more or less equivalent, and with more compact syntax).
> > 
> > I’m hoping that this might be low-hanging fruit that could be considered for the Swift 4 release. I’d be happy to have a go at writing a proposal if there’s interest!
> > 
> > –Adam
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Michael\_J\_LeHew\_Jr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michael_j_lehew_jr/32/2615_2.png) [@Michael\_J\_LeHew\_Jr](https://forums.swift.org/u/Michael_J_LeHew_Jr)\
**Post date:** [June 7, 2017, 11:36pm UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/4 "2017-06-07T23:36:54Z")

</div>

This is a great idea, and ought to be easy enough to bring forward! +1 from me!

-Michael

> **···**
>
> > On Jun 7, 2017, at 11:18 AM, Matt Diephouse via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > 💯
> > 
> > > On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > 
> > > The new smart key path feature is really lovely, and feels like a great addition to Swift.
> > > 
> > > It seems like it might be straightforward to add overloads of `map` and `flatMap` to the standard library to make use of the new functionality:
> > > 
> > > &nbsp;&nbsp;let managers = flatOrganisation.managers  
> > > &nbsp;&nbsp;let allEmployees = Set(managers.flatMap(\.directReports))  
> > > &nbsp;&nbsp;let employeeNames = Set(allEmployees.map(\.name))
> > > 
> > > This feels like a really natural way of working with key paths in a functional style. It makes a lot of sense for collections, and possibly for Optional too (although as far as I can see optional chaining is more or less equivalent, and with more compact syntax).
> > > 
> > > I’m hoping that this might be low-hanging fruit that could be considered for the Swift 4 release. I’d be happy to have a go at writing a proposal if there’s interest!
> > > 
> > > –Adam
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![victorattuenti](https://avatars.discourse-cdn.com/v4/letter/v/76d3ee/32.png) [@victorattuenti](https://forums.swift.org/u/victorattuenti)\
**Post date:** [June 7, 2017, 6:15pm UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/5 "2017-06-07T18:15:38Z")

</div>

+1, it seems like a wonderful addition that would make a lot of code much  
shorter.

> **···**
>
> On Wed, Jun 7, 2017 at 7:35 PM, Adam Sharp via swift-evolution \< swift-evolution@swift.org\> wrote:
> 
> > The new smart key path feature is really lovely, and feels like a great  
> > addition to Swift.
> > 
> > It seems like it might be straightforward to add overloads of `map` and  
> > `flatMap` to the standard library to make use of the new functionality:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let managers = flatOrganisation.managers  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let allEmployees = Set(managers.flatMap(\.directReports))  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let employeeNames = Set(allEmployees.map(\.name))
> > 
> > This feels like a really natural way of working with key paths in a  
> > functional style. It makes a lot of sense for collections, and possibly for  
> > Optional too (although as far as I can see optional chaining is more or  
> > less equivalent, and with more compact syntax).
> > 
> > I’m hoping that this might be low-hanging fruit that could be considered  
> > for the Swift 4 release. I’d be happy to have a go at writing a proposal if  
> > there’s interest!
> > 
> > –Adam
> 
> --  
> Víctor Pimentel

---

<div class="post-metadata">

**Author:** ![tobias](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tobias/32/28982_2.png) [@tobias](https://forums.swift.org/u/tobias)\
**Post date:** [June 8, 2017, 2:20pm UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/6 "2017-06-08T14:20:53Z")

</div>

I had the need for the same thing today and made it as an extension on Sequence.

extension Sequence {  
&nbsp;&nbsp;&nbsp;&nbsp;func map\<T\>(\_ keyPath: KeyPath\<Element, T\>) -\> [T] {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self.map {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$0[keyPath: keyPath]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;func flatMap\<T\>(\_ keyPath: KeyPath\<Element, T?\>) -\> [T] {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self.flatMap {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$0[keyPath: keyPath]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

> **···**
>
> > The new smart key path feature is really lovely, and feels like a great addition to Swift.
> > 
> > It seems like it might be straightforward to add overloads of `map` and `flatMap` to the standard library to make use of the new functionality:
> > 
> > let managers = flatOrganisation.managers  
> > let allEmployees = Set(managers.flatMap(\.directReports))  
> > let employeeNames = Set(allEmployees.map(\.name))
> > 
> > This feels like a really natural way of working with key paths in a functional style. It makes a lot of sense for collections, and possibly for Optional too (although as far as I can see optional chaining is more or less equivalent, and with more compact syntax).
> > 
> > I’m hoping that this might be low-hanging fruit that could be considered for the Swift 4 release. I’d be happy to have a go at writing a proposal if there’s interest!
> > 
> > –Adam

---

<div class="post-metadata">

**Author:** ![moiseev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/moiseev/32/281_2.png) [@moiseev](https://forums.swift.org/u/moiseev)\
**Post date:** [June 9, 2017, 11:06pm UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/7 "2017-06-09T23:06:55Z")

</div>

Sorry. I might be missing something. Why is this better than:

let allEmployees = Set(managers.flatMap { $0.directReports }

?

> **···**
>
> > On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > The new smart key path feature is really lovely, and feels like a great addition to Swift.
> > 
> > It seems like it might be straightforward to add overloads of `map` and `flatMap` to the standard library to make use of the new functionality:
> > 
> > &nbsp;&nbsp;let managers = flatOrganisation.managers  
> > &nbsp;&nbsp;let allEmployees = Set(managers.flatMap(\.directReports))  
> > &nbsp;&nbsp;let employeeNames = Set(allEmployees.map(\.name))
> > 
> > This feels like a really natural way of working with key paths in a functional style. It makes a lot of sense for collections, and possibly for Optional too (although as far as I can see optional chaining is more or less equivalent, and with more compact syntax).
> > 
> > I’m hoping that this might be low-hanging fruit that could be considered for the Swift 4 release. I’d be happy to have a go at writing a proposal if there’s interest!
> > 
> > –Adam
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [June 10, 2017, 12:16am UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/8 "2017-06-10T00:16:23Z")

</div>

Working demo:

struct VirtualKeyPath\<Root, Value\> {  
&nbsp;&nbsp;&nbsp;&nbsp;let block: (Root) -\> Value  
&nbsp;&nbsp;&nbsp;&nbsp;func evaluate(on: Root) -\> Value { return block(on) }  
}

// If we could extend 'Any', this would be possible...  
//extension Any {  
// subscript(keyPath: VirtualKeyPath\<Self, Value\>) -\> Value {  
// return keyPath.evaluate(on: self)  
// }  
//}

extension KeyPath where Value: Collection {  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;func map\<T\>(\_ descendent: KeyPath\<Value.Element, T\>) -\> VirtualKeyPath\<Root, [T]\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return VirtualKeyPath\<Root, [T]\> { (obj: Root) -\> [T] in  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return obj[keyPath: self].map { $0[keyPath: descendent] }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

extension VirtualKeyPath where Value: Collection {  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;func map\<T\>(\_ descendent: KeyPath\<Value.Element, T\>) -\> VirtualKeyPath\<Root, [T]\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return VirtualKeyPath\<Root, [T]\> { (obj: Root) -\> [T] in  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self.evaluate(on: obj).map { $0[keyPath: descendent] }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

struct Person {  
&nbsp;&nbsp;&nbsp;&nbsp;let name: String  
}  
struct Department {  
&nbsp;&nbsp;&nbsp;&nbsp;let people: [Person]  
}

let nameLengths = (\Department.people).map(\.name).map(\.characters.count)

let testObj = Department(people: [Person(name: "Alice"),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person(name: "Bob"),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person(name: "Claire"),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Person(name: "David")])

kp.evaluate(on: testObj) // returns [5, 3, 6, 5]  
As far as making this kind of thing easier in the language is concerned, one thing I can think of is allowing another \ to end the key-path expression, rather than enclosing it with brackets. So:

let nameLengths = (\Department.people).map(\.name).map(\.characters.count)

Becomes:

let nameLengths = \Department.people\.map(\.name).map(\.characters.count)

And that’s it, I think. It’s quite nice as-is.

- Karl

> **···**
>
> > On 7. Jun 2017, at 19:35, Adam Sharp via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > The new smart key path feature is really lovely, and feels like a great addition to Swift.
> > 
> > It seems like it might be straightforward to add overloads of `map` and `flatMap` to the standard library to make use of the new functionality:
> > 
> > &nbsp;&nbsp;let managers = flatOrganisation.managers  
> > &nbsp;&nbsp;let allEmployees = Set(managers.flatMap(\.directReports))  
> > &nbsp;&nbsp;let employeeNames = Set(allEmployees.map(\.name))
> > 
> > This feels like a really natural way of working with key paths in a functional style. It makes a lot of sense for collections, and possibly for Optional too (although as far as I can see optional chaining is more or less equivalent, and with more compact syntax).
> > 
> > I’m hoping that this might be low-hanging fruit that could be considered for the Swift 4 release. I’d be happy to have a go at writing a proposal if there’s interest!
> > 
> > –Adam
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [June 8, 2017, 1:11am UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/9 "2017-06-08T01:11:11Z")

</div>

+1. Would think that all variants should exist on Optional too unless it  
would be harmful.

> **···**
>
> On Wed, Jun 7, 2017 at 20:13 Michael J LeHew Jr via swift-evolution \< swift-evolution@swift.org\> wrote:
> 
> > This is a great idea, and ought to be easy enough to bring forward! +1  
> > from me!
> > 
> > -Michael
> > 
> > \> On Jun 7, 2017, at 11:18 AM, Matt Diephouse via swift-evolution \< \> swift-evolution@swift.org\> wrote:  
> > \>  
> > \> 💯  
> > \>  
> > \>\> On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution \< \> swift-evolution@swift.org\> wrote:  
> > \>\>  
> > \>\> The new smart key path feature is really lovely, and feels like a great  
> > addition to Swift.  
> > \>\>  
> > \>\> It seems like it might be straightforward to add overloads of `map` and  
> > `flatMap` to the standard library to make use of the new functionality:  
> > \>\>  
> > \>\> let managers = flatOrganisation.managers  
> > \>\> let allEmployees = Set(managers.flatMap(\.directReports))  
> > \>\> let employeeNames = Set(allEmployees.map(\.name))  
> > \>\>  
> > \>\> This feels like a really natural way of working with key paths in a  
> > functional style. It makes a lot of sense for collections, and possibly for  
> > Optional too (although as far as I can see optional chaining is more or  
> > less equivalent, and with more compact syntax).  
> > \>\>  
> > \>\> I’m hoping that this might be low-hanging fruit that could be  
> > considered for the Swift 4 release. I’d be happy to have a go at writing a  
> > proposal if there’s interest!  
> > \>\>  
> > \>\> –Adam  
> > \>\>  
> > \>\> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > \>\> swift-evolution mailing list  
> > \>\> swift-evolution@swift.org  
> > \>\> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > \>  
> > \> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > \> swift-evolution mailing list  
> > \> swift-evolution@swift.org  
> > \> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![saagarjha](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@saagarjha](https://forums.swift.org/u/saagarjha)\
**Post date:** [June 9, 2017, 11:54pm UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/10 "2017-06-09T23:54:54Z")

</div>

It reads better and feels more natural. In my mind, it’s similar to the difference between

["1", "2", "3"].flatMap { Double($0) }

and

["1", "2", "3"].flatMap(Double.init)

Saagar Jha

> **···**
>
> > On Jun 9, 2017, at 16:06, Max Moiseev via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > Sorry. I might be missing something. Why is this better than:
> > 
> > let allEmployees = Set(managers.flatMap { $0.directReports }
> > 
> > ?
> > 
> > > On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > 
> > > The new smart key path feature is really lovely, and feels like a great addition to Swift.
> > > 
> > > It seems like it might be straightforward to add overloads of `map` and `flatMap` to the standard library to make use of the new functionality:
> > > 
> > > &nbsp;&nbsp;let managers = flatOrganisation.managers  
> > > &nbsp;&nbsp;let allEmployees = Set(managers.flatMap(\.directReports))  
> > > &nbsp;&nbsp;let employeeNames = Set(allEmployees.map(\.name))
> > > 
> > > This feels like a really natural way of working with key paths in a functional style. It makes a lot of sense for collections, and possibly for Optional too (although as far as I can see optional chaining is more or less equivalent, and with more compact syntax).
> > > 
> > > I’m hoping that this might be low-hanging fruit that could be considered for the Swift 4 release. I’d be happy to have a go at writing a proposal if there’s interest!
> > > 
> > > –Adam
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![hartbit](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hartbit/32/738_2.png) [@hartbit](https://forums.swift.org/u/hartbit)\
**Post date:** [June 10, 2017, 7:40pm UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/11 "2017-06-10T19:40:38Z")

</div>

> Sorry. I might be missing something. Why is this better than:
> 
> let allEmployees = Set(managers.flatMap { $0.directReports }
> 
> ?

For the same reasons

managers.flatMap(calculateReports(\_:))

Is better than:

managers.flatMap { calculateReports($0) }

Quite a few people enjoy this tearse, functional style.

> **···**
>
> > On 9 Jun 2017, at 19:06, Max Moiseev via swift-evolution \<swift-evolution@swift.org\> wrote:
> 
> > > On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > 
> > > The new smart key path feature is really lovely, and feels like a great addition to Swift.
> > > 
> > > It seems like it might be straightforward to add overloads of `map` and `flatMap` to the standard library to make use of the new functionality:
> > > 
> > > &nbsp;&nbsp;&nbsp;let managers = flatOrganisation.managers  
> > > &nbsp;&nbsp;&nbsp;let allEmployees = Set(managers.flatMap(\.directReports))  
> > > &nbsp;&nbsp;&nbsp;let employeeNames = Set(allEmployees.map(\.name))
> > > 
> > > This feels like a really natural way of working with key paths in a functional style. It makes a lot of sense for collections, and possibly for Optional too (although as far as I can see optional chaining is more or less equivalent, and with more compact syntax).
> > > 
> > > I’m hoping that this might be low-hanging fruit that could be considered for the Swift 4 release. I’d be happy to have a go at writing a proposal if there’s interest!
> > > 
> > > –Adam
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [June 8, 2017, 2:58am UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/12 "2017-06-08T02:58:52Z")

</div>

+1, I really like this. It would also align nicely with the method type  
flattening in SE-0042  
\<[https://github.com/apple/swift-evolution/blob/master/proposals/0042-flatten-method-types.md&gt](https://github.com/apple/swift-evolution/blob/master/proposals/0042-flatten-method-types.md&gt);  
(once it gets implemented), because passing keypaths (i.e., unbound  
property references) and unbound parameterless method references to  
map/flatMap would look nearly the same:

```swift
struct Person {
  let firstName: String
  let lastName: String
  func fullName() -> String { return "\(firstName) \(lastName)" }
}

let people: [Person]
let firstNames = people.map(\.firstName)
let fullNames = people.map(Person.fullName) // because after SE-0042, this
will be (Person) -> String, not (Person) -> () -> String

```

Especially if there's a move in the future to also use \. to denote unbound  
methods references, which was discussed during the keypath reviews. (Even  
with that, I believe it would be more work though to get rid of the  
explicit type name in the function case.)

> **···**
>
> On Wed, Jun 7, 2017 at 6:11 PM Xiaodi Wu via swift-evolution \< swift-evolution@swift.org\> wrote:
> 
> > +1. Would think that all variants should exist on Optional too unless it  
> > would be harmful.  
> > On Wed, Jun 7, 2017 at 20:13 Michael J LeHew Jr via swift-evolution \< \> swift-evolution@swift.org\> wrote:
> > 
> > > This is a great idea, and ought to be easy enough to bring forward! +1  
> > > from me!
> > > 
> > > -Michael
> > > 
> > > \> On Jun 7, 2017, at 11:18 AM, Matt Diephouse via swift-evolution \< \>\> swift-evolution@swift.org\> wrote:  
> > > \>  
> > > \> 💯  
> > > \>  
> > > \>\> On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution \< \>\> swift-evolution@swift.org\> wrote:  
> > > \>\>  
> > > \>\> The new smart key path feature is really lovely, and feels like a  
> > > great addition to Swift.  
> > > \>\>  
> > > \>\> It seems like it might be straightforward to add overloads of `map`  
> > > and `flatMap` to the standard library to make use of the new functionality:  
> > > \>\>  
> > > \>\> let managers = flatOrganisation.managers  
> > > \>\> let allEmployees = Set(managers.flatMap(\.directReports))  
> > > \>\> let employeeNames = Set(allEmployees.map(\.name))  
> > > \>\>  
> > > \>\> This feels like a really natural way of working with key paths in a  
> > > functional style. It makes a lot of sense for collections, and possibly for  
> > > Optional too (although as far as I can see optional chaining is more or  
> > > less equivalent, and with more compact syntax).  
> > > \>\>  
> > > \>\> I’m hoping that this might be low-hanging fruit that could be  
> > > considered for the Swift 4 release. I’d be happy to have a go at writing a  
> > > proposal if there’s interest!  
> > > \>\>  
> > > \>\> –Adam  
> > > \>\>  
> > > \>\> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > \>\> swift-evolution mailing list  
> > > \>\> swift-evolution@swift.org  
> > > \>\> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > > \>  
> > > \> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > \> swift-evolution mailing list  
> > > \> swift-evolution@swift.org  
> > > \> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![dabrahams](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dabrahams/32/9815_2.png) [@dabrahams](https://forums.swift.org/u/dabrahams)\
**Post date:** [June 10, 2017, 3:29am UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/13 "2017-06-10T03:29:14Z")

</div>

> It reads better and feels more natural. In my mind, it’s similar to the difference between
> 
> ["1", "2", "3"].flatMap { Double($0) }
> 
> and
> 
> ["1", "2", "3"].flatMap(Double.init)
> 
> Saagar Jha

I am not convinced this syntactic sugar is worth complicating the  
language or library for, but if it is, IMO the right thing is to make a  
keypath be-a subtype of the appropriate function type, rather than to  
start down the path of creating a keypath overload for every method that  
takes a closure argument.

> **···**
>
> on Fri Jun 09 2017, Saagar Jha \<swift-evolution@swift.org\> wrote:
> 
> > > On Jun 9, 2017, at 16:06, Max Moiseev via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > 
> > > Sorry. I might be missing something. Why is this better than:
> > > 
> > > let allEmployees = Set(managers.flatMap { $0.directReports }
> > > 
> > > ?
> > > 
> > > > On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution \>\>\> \<swift-evolution@swift.org\> wrote:
> > > > 
> > > > The new smart key path feature is really lovely, and feels like a great addition to Swift.
> > > > 
> > > > It seems like it might be straightforward to add overloads of `map`  
> > > > and `flatMap` to the standard library to make use of the new  
> > > > functionality:
> > > > 
> > > > &nbsp;&nbsp;let managers = flatOrganisation.managers  
> > > > &nbsp;&nbsp;let allEmployees = Set(managers.flatMap(\.directReports))  
> > > > &nbsp;&nbsp;let employeeNames = Set(allEmployees.map(\.name))
> > > > 
> > > > This feels like a really natural way of working with key paths in a  
> > > > functional style. It makes a lot of sense for collections, and  
> > > > possibly for Optional too (although as far as I can see optional  
> > > > chaining is more or less equivalent, and with more compact syntax).
> > > > 
> > > > I’m hoping that this might be low-hanging fruit that could be  
> > > > considered for the Swift 4 release. I’d be happy to have a go at  
> > > > writing a proposal if there’s interest!
> > > > 
> > > > –Adam
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> 
> --  
> -Dave

---

<div class="post-metadata">

**Author:** ![Paul\_Cantrell](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paul_cantrell/32/16764_2.png) [@Paul\_Cantrell](https://forums.swift.org/u/Paul_Cantrell)\
**Post date:** [June 8, 2017, 4:19am UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/14 "2017-06-08T04:19:29Z")

</div>

It should be possible to achieve Ruby-like generality in Swift with a protocol for “thing that can converted to a transform function.” That wouldn’t need a special & operator.

Here’s a sketch. This sketch doesn’t compile — maybe not enough of Swift 4 is there yet for it to work, or maybe I am missing something obvious and need to go to sleep now — but it’s close enough to suggest the approach:

&nbsp;&nbsp;&nbsp;&nbsp;public protocol TransformConvertible { // or whatever you want to call it  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;associatedtype From  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;associatedtype To  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var transform: (From) -\> To { get }  
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;extension KeyPath: TransformConvertible {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public typealias From = Root  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public typealias To = Value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public var transform: (Root) -\> Value {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return { $0[keypath: self] }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;extension Sequence {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public func map\<T, U\>(\_ transformSource: U) -\> [T]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where U: TransformConvertible,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U.From == Element,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U.To == T {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return map(transformSource.transform)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;}

This seems a bit more ambitious, perhaps not suitable for this round of Swift evolution work. But I throw it out there at least to show that supporting people.map(\.firstName) today would not preclude a generic keypath → function mechanism in the future:

A flavor of map that accepts a keypath today could be generalized to accept TransformConvertible in the future without breaking existing code.  
When calling a function that doesn’t know how to work with TransformConvertible, you could use (Foo.bar).transform, no special operator needed.

Cheers,

Paul

P.S. Largely irrelevant Ruby aside: Ruby’s & is not a free-floating operator, but part of the method invocation syntax indicating that the following arg should be treated as a block. Ruby calls a to\_proc method on whatever is in that position. Symbol implements to\_proc by returning a lambda that calls the method named by the symbol on the lambda’s first arg. Very much the duck-typed version of TransformConvertible above.

> **···**
>
> > On Jun 7, 2017, at 10:21 PM, Stephen Celis via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > -1
> > 
> > A -1 from me may be surprising. I'm excited about key path composition and generic solutions, e.g. this experiment with lenses: [https://twitter.com/stephencelis/status/863916921577758721](https://twitter.com/stephencelis/status/863916921577758721)
> > 
> > But I'd prefer a reusable solution for converting key paths into functions.
> > 
> > Heaven help me for this Rubyism, but a prefix "&" operator (or, maybe better yet, some implicit mechanism) could convert a key-path to a function that passes a root value to a key path...
> > 
> > &nbsp;&nbsp;people.map(&\.firstName)
> > 
> > This way any function that takes a transformation from "whole" to "part" could take a key path. Requiring an overload per instance is less flexible.
> > 
> > Stephen
> > 
> > > On Jun 7, 2017, at 10:58 PM, Tony Allevato via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > 
> > > +1, I really like this. It would also align nicely with the method type flattening in SE-0042 (once it gets implemented), because passing keypaths (i.e., unbound property references) and unbound parameterless method references to map/flatMap would look nearly the same:
> > > 
> > > ```swift
> > > struct Person {
> > > let firstName: String
> > > let lastName: String
> > > func fullName() -> String { return "\(firstName) \(lastName)" }
> > > }
> > > 
> > > let people: [Person]
> > > let firstNames = people.map(\.firstName)
> > > let fullNames = people.map(Person.fullName) // because after SE-0042, this will be (Person) -> String, not (Person) -> () -> String
> > > 
> > > ```
> > > 
> > > Especially if there's a move in the future to also use \. to denote unbound methods references, which was discussed during the keypath reviews. (Even with that, I believe it would be more work though to get rid of the explicit type name in the function case.)
> > > 
> > > On Wed, Jun 7, 2017 at 6:11 PM Xiaodi Wu via swift-evolution \<swift-evolution@swift.org\> wrote:  
> > > +1. Would think that all variants should exist on Optional too unless it would be harmful.  
> > > On Wed, Jun 7, 2017 at 20:13 Michael J LeHew Jr via swift-evolution \<swift-evolution@swift.org\> wrote:  
> > > This is a great idea, and ought to be easy enough to bring forward! +1 from me!
> > > 
> > > -Michael
> > > 
> > > > On Jun 7, 2017, at 11:18 AM, Matt Diephouse via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > > 
> > > > 💯
> > > > 
> > > > > On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > > > 
> > > > > The new smart key path feature is really lovely, and feels like a great addition to Swift.
> > > > > 
> > > > > It seems like it might be straightforward to add overloads of `map` and `flatMap` to the standard library to make use of the new functionality:
> > > > > 
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;let managers = flatOrganisation.managers  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;let allEmployees = Set(managers.flatMap(\.directReports))  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;let employeeNames = Set(allEmployees.map(\.name))
> > > > > 
> > > > > This feels like a really natural way of working with key paths in a functional style. It makes a lot of sense for collections, and possibly for Optional too (although as far as I can see optional chaining is more or less equivalent, and with more compact syntax).
> > > > > 
> > > > > I’m hoping that this might be low-hanging fruit that could be considered for the Swift 4 release. I’d be happy to have a go at writing a proposal if there’s interest!
> > > > > 
> > > > > –Adam
> > > > > 
> > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > swift-evolution mailing list  
> > > > > swift-evolution@swift.org  
> > > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![stephencelis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stephencelis/32/2675_2.png) [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Post date:** [June 8, 2017, 3:21am UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/15 "2017-06-08T03:21:17Z")

</div>

-1

A -1 from me may be surprising. I'm excited about key path composition and generic solutions, e.g. this experiment with lenses: [https://twitter.com/stephencelis/status/863916921577758721](https://twitter.com/stephencelis/status/863916921577758721)

But I'd prefer a reusable solution for converting key paths into functions.

Heaven help me for this Rubyism, but a prefix "&" operator (or, maybe better yet, some implicit mechanism) could convert a key-path to a function that passes a root value to a key path...

&nbsp;&nbsp;&nbsp;people.map(&\.firstName)

This way any function that takes a transformation from "whole" to "part" could take a key path. Requiring an overload per instance is less flexible.

Stephen

> **···**
>
> > On Jun 7, 2017, at 10:58 PM, Tony Allevato via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > +1, I really like this. It would also align nicely with the method type flattening in SE-0042 (once it gets implemented), because passing keypaths (i.e., unbound property references) and unbound parameterless method references to map/flatMap would look nearly the same:
> > 
> > ```swift
> > struct Person {
> > let firstName: String
> > let lastName: String
> > func fullName() -> String { return "\(firstName) \(lastName)" }
> > }
> > 
> > let people: [Person]
> > let firstNames = people.map(\.firstName)
> > let fullNames = people.map(Person.fullName) // because after SE-0042, this will be (Person) -> String, not (Person) -> () -> String
> > 
> > ```
> > 
> > Especially if there's a move in the future to also use \. to denote unbound methods references, which was discussed during the keypath reviews. (Even with that, I believe it would be more work though to get rid of the explicit type name in the function case.)
> > 
> > On Wed, Jun 7, 2017 at 6:11 PM Xiaodi Wu via swift-evolution \<swift-evolution@swift.org\> wrote:  
> > +1. Would think that all variants should exist on Optional too unless it would be harmful.  
> > On Wed, Jun 7, 2017 at 20:13 Michael J LeHew Jr via swift-evolution \<swift-evolution@swift.org\> wrote:  
> > This is a great idea, and ought to be easy enough to bring forward! +1 from me!
> > 
> > -Michael
> > 
> > \> On Jun 7, 2017, at 11:18 AM, Matt Diephouse via swift-evolution \<swift-evolution@swift.org\> wrote:  
> > \>  
> > \> 💯  
> > \>  
> > \>\> On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution \<swift-evolution@swift.org\> wrote:  
> > \>\>  
> > \>\> The new smart key path feature is really lovely, and feels like a great addition to Swift.  
> > \>\>  
> > \>\> It seems like it might be straightforward to add overloads of `map` and `flatMap` to the standard library to make use of the new functionality:  
> > \>\>  
> > \>\> let managers = flatOrganisation.managers  
> > \>\> let allEmployees = Set(managers.flatMap(\.directReports))  
> > \>\> let employeeNames = Set(allEmployees.map(\.name))  
> > \>\>  
> > \>\> This feels like a really natural way of working with key paths in a functional style. It makes a lot of sense for collections, and possibly for Optional too (although as far as I can see optional chaining is more or less equivalent, and with more compact syntax).  
> > \>\>  
> > \>\> I’m hoping that this might be low-hanging fruit that could be considered for the Swift 4 release. I’d be happy to have a go at writing a proposal if there’s interest!  
> > \>\>  
> > \>\> –Adam  
> > \>\>  
> > \>\> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > \>\> swift-evolution mailing list  
> > \>\> swift-evolution@swift.org  
> > \>\> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > \>  
> > \> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > \> swift-evolution mailing list  
> > \> swift-evolution@swift.org  
> > \> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![SusanCheng](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/susancheng/32/9439_2.png) [@SusanCheng](https://forums.swift.org/u/SusanCheng)\
**Post date:** [June 8, 2017, 4:34am UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/16 "2017-06-08T04:34:43Z")

</div>

this work,

prefix operator \*

prefix func \*\<Root, Value\>(keyPath: KeyPath\<Root, Value\>) -\> (Root) -\> Value  
{

&nbsp;&nbsp;&nbsp;&nbsp;return { $0[keyPath: keyPath] }

}

["Hello, World"].map(\*\String.count) // [12]

> **···**
>
> 2017-06-08 12:19 GMT+08:00 Paul Cantrell via swift-evolution \< swift-evolution@swift.org\>:
> 
> > It should be possible to achieve Ruby-like generality in Swift with a  
> > protocol for “thing that can converted to a transform function.” That  
> > wouldn’t need a special & operator.
> > 
> > Here’s a sketch. This sketch doesn’t compile — maybe not enough of Swift 4  
> > is there yet for it to work, or maybe I am missing something obvious and  
> > need to go to sleep now — but it’s close enough to suggest the approach:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;public protocol TransformConvertible { // or whatever you want to  
> > call it  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;associatedtype From  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;associatedtype To
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var transform: (From) -\> To { get }  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;extension KeyPath: TransformConvertible {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public typealias From = Root  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public typealias To = Value
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public var transform: (Root) -\> Value {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return { $0[keypath: self] }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;extension Sequence {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public func map\<T, U\>(\_ transformSource: U) -\> [T]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where U: TransformConvertible,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U.From == Element,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U.To == T {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return map(transformSource.transform)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > This seems a bit more ambitious, perhaps not suitable for this round of  
> > Swift evolution work. But I throw it out there at least to show that  
> > supporting people.map(\.firstName) today \*would not preclude\* a generic  
> > keypath → function mechanism in the future:
> > 
> > &nbsp;&nbsp;&nbsp;- A flavor of map that accepts a keypath today could be generalized to  
> > &nbsp;&nbsp;&nbsp;accept TransformConvertible in the future without breaking existing code.  
> > &nbsp;&nbsp;&nbsp;- When calling a function that doesn’t know how to work  
> > &nbsp;&nbsp;&nbsp;with TransformConvertible, you could use (Foo.bar).transform, no special  
> > &nbsp;&nbsp;&nbsp;operator needed.
> > 
> > Cheers,
> > 
> > Paul
> > 
> > P.S. Largely irrelevant Ruby aside: Ruby’s & is not a free-floating  
> > operator, but part of the method invocation syntax indicating that the  
> > following arg should be treated as a block. Ruby calls a to\_proc method on  
> > whatever is in that position. Symbol implements to\_proc by returning a  
> > lambda that calls the method named by the symbol on the lambda’s first arg.  
> > Very much the duck-typed version of TransformConvertible above.
> > 
> > On Jun 7, 2017, at 10:21 PM, Stephen Celis via swift-evolution \< \> swift-evolution@swift.org\> wrote:
> > 
> > -1
> > 
> > A -1 from me may be surprising. I'm excited about key path composition and  
> > generic solutions, e.g. this experiment with lenses: [https://twitter.com/](https://twitter.com/)  
> > stephencelis/status/863916921577758721
> > 
> > But I'd prefer a reusable solution for converting key paths into functions.
> > 
> > Heaven help me for this Rubyism, but a prefix "&" operator (or, maybe  
> > better yet, some implicit mechanism) could convert a key-path to a function  
> > that passes a root value to a key path...
> > 
> > &nbsp;&nbsp;people.map(&\.firstName)
> > 
> > This way any function that takes a transformation from "whole" to "part"  
> > could take a key path. Requiring an overload per instance is less flexible.
> > 
> > Stephen
> > 
> > On Jun 7, 2017, at 10:58 PM, Tony Allevato via swift-evolution \< \> swift-evolution@swift.org\> wrote:
> > 
> > +1, I really like this. It would also align nicely with the method type  
> > flattening in SE-0042 (once it gets implemented), because passing keypaths  
> > (i.e., unbound property references) and unbound parameterless method  
> > references to map/flatMap would look nearly the same:
> > 
> > ```swift
> > struct Person {
> > let firstName: String
> > let lastName: String
> > func fullName() -> String { return "\(firstName) \(lastName)" }
> > }
> > 
> > let people: [Person]
> > let firstNames = people.map(\.firstName)
> > let fullNames = people.map(Person.fullName) // because after SE-0042,
> > this will be (Person) -> String, not (Person) -> () -> String
> > 
> > ```
> > 
> > Especially if there's a move in the future to also use \. to denote  
> > unbound methods references, which was discussed during the keypath reviews.  
> > (Even with that, I believe it would be more work though to get rid of the  
> > explicit type name in the function case.)
> > 
> > On Wed, Jun 7, 2017 at 6:11 PM Xiaodi Wu via swift-evolution \< \> swift-evolution@swift.org\> wrote:  
> > +1. Would think that all variants should exist on Optional too unless it  
> > would be harmful.  
> > On Wed, Jun 7, 2017 at 20:13 Michael J LeHew Jr via swift-evolution \< \> swift-evolution@swift.org\> wrote:  
> > This is a great idea, and ought to be easy enough to bring forward! +1  
> > from me!
> > 
> > -Michael
> > 
> > On Jun 7, 2017, at 11:18 AM, Matt Diephouse via swift-evolution \< \> swift-evolution@swift.org\> wrote:
> > 
> > 💯
> > 
> > On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution \< \> swift-evolution@swift.org\> wrote:
> > 
> > The new smart key path feature is really lovely, and feels like a great  
> > addition to Swift.
> > 
> > It seems like it might be straightforward to add overloads of `map` and  
> > `flatMap` to the standard library to make use of the new functionality:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;let managers = flatOrganisation.managers  
> > &nbsp;&nbsp;&nbsp;&nbsp;let allEmployees = Set(managers.flatMap(\.directReports))  
> > &nbsp;&nbsp;&nbsp;&nbsp;let employeeNames = Set(allEmployees.map(\.name))
> > 
> > This feels like a really natural way of working with key paths in a  
> > functional style. It makes a lot of sense for collections, and possibly for  
> > Optional too (although as far as I can see optional chaining is more or  
> > less equivalent, and with more compact syntax).
> > 
> > I’m hoping that this might be low-hanging fruit that could be considered  
> > for the Swift 4 release. I’d be happy to have a go at writing a proposal if  
> > there’s interest!
> > 
> > –Adam
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [June 9, 2017, 11:42pm UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/17 "2017-06-09T23:42:41Z")

</div>

> +1, I really like this. It would also align nicely with the method type flattening in SE-0042 \<[https://github.com/apple/swift-evolution/blob/master/proposals/0042-flatten-method-types.md&gt](https://github.com/apple/swift-evolution/blob/master/proposals/0042-flatten-method-types.md&gt); (once it gets implemented), because passing keypaths (i.e., unbound property references) and unbound parameterless method references to map/flatMap would look nearly the same:
> 
> ```swift
> struct Person {
> let firstName: String
> let lastName: String
> func fullName() -> String { return "\(firstName) \(lastName)" }
> }
> 
> let people: [Person]
> let firstNames = people.map(\.firstName)
> let fullNames = people.map(Person.fullName) // because after SE-0042, this will be (Person) -> String, not (Person) -> () -> String
> 
> ```
> 
> Especially if there's a move in the future to also use \. to denote unbound methods references, which was discussed during the keypath reviews. (Even with that, I believe it would be more work though to get rid of the explicit type name in the function case.)

When it comes to unbound method references, personally, I would love to see us ditch currying in some future version of Swift and move to full-blown partial application instead. We would need variadic generics if we wanted to expose them as nicely-typed objects as we do with KeyPaths.

Anyway, I think what you want is something like this (where VirtualKeyPath is a custom subclass of KeyPath which is lazily-evaluated using a closure).

extension KeyPath where Value: Collection {  
&nbsp;&nbsp;&nbsp;&nbsp;func map\<T\>(\_ descendent: KeyPath\<Value, T\>) -\> VirtualKeyPath\<Root, [T]\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return VirtualKeyPath\<Root, [T]\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(obj: Root) -\> [T] in obj[keypath: self].map { $0[keypath: descendent] }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

\Department.people.map(\.fullName).characters.count // type: VirtualKeyPath\<Department, [Int]\>

Custom subclasses of KeyPath are not allowed, so you can’t actually do this. I don’t know, maybe it wouldn’t be much overhead to add the one, closure-based VirtualKeyPath — clearly the architecture is meant to be flexible. Maybe it’s better to wait until Swift 5 for that, though.

- Karl

> **···**
>
> > On 8. Jun 2017, at 04:58, Tony Allevato via swift-evolution \<swift-evolution@swift.org\> wrote:
> 
> > On Wed, Jun 7, 2017 at 6:11 PM Xiaodi Wu via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:  
> > +1. Would think that all variants should exist on Optional too unless it would be harmful.  
> > On Wed, Jun 7, 2017 at 20:13 Michael J LeHew Jr via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:  
> > This is a great idea, and ought to be easy enough to bring forward! +1 from me!
> > 
> > -Michael
> > 
> > \> On Jun 7, 2017, at 11:18 AM, Matt Diephouse via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:  
> > \>  
> > \> 💯  
> > \>  
> > \>\> On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:  
> > \>\>  
> > \>\> The new smart key path feature is really lovely, and feels like a great addition to Swift.  
> > \>\>  
> > \>\> It seems like it might be straightforward to add overloads of `map` and `flatMap` to the standard library to make use of the new functionality:  
> > \>\>  
> > \>\> let managers = flatOrganisation.managers  
> > \>\> let allEmployees = Set(managers.flatMap(\.directReports))  
> > \>\> let employeeNames = Set(allEmployees.map(\.name))  
> > \>\>  
> > \>\> This feels like a really natural way of working with key paths in a functional style. It makes a lot of sense for collections, and possibly for Optional too (although as far as I can see optional chaining is more or less equivalent, and with more compact syntax).  
> > \>\>  
> > \>\> I’m hoping that this might be low-hanging fruit that could be considered for the Swift 4 release. I’d be happy to have a go at writing a proposal if there’s interest!  
> > \>\>  
> > \>\> –Adam  
> > \>\>  
> > \>\> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > \>\> swift-evolution mailing list  
> > \>\> swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > \>\> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > \>  
> > \> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > \> swift-evolution mailing list  
> > \> swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > \> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Paul\_Cantrell](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paul_cantrell/32/16764_2.png) [@Paul\_Cantrell](https://forums.swift.org/u/Paul_Cantrell)\
**Post date:** [June 8, 2017, 4:40am UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/18 "2017-06-08T04:40:45Z")

</div>

Ah, quite ingenious, Susan!

And you made me realize that the only reason my no-operator-needed code didn’t compile is that I had keypath instead of keyPath. So it was indeed “Paul needs to go to sleep now” option, which I will now do. 🙄

P

> **···**
>
> > On Jun 7, 2017, at 11:34 PM, Susan Cheng \<susan.doggie@gmail.com\> wrote:
> > 
> > this work,
> > 
> > prefix operator \*
> > 
> > prefix func \*\<Root, Value\>(keyPath: KeyPath\<Root, Value\>) -\> (Root) -\> Value {  
> > &nbsp;&nbsp;&nbsp;&nbsp;return { $0[keyPath: keyPath] }  
> > }
> > 
> > ["Hello, World"].map(\*\String.count) // [12]
> > 
> > 2017-06-08 12:19 GMT+08:00 Paul Cantrell via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\>:  
> > It should be possible to achieve Ruby-like generality in Swift with a protocol for “thing that can converted to a transform function.” That wouldn’t need a special & operator.
> > 
> > Here’s a sketch. This sketch doesn’t compile — maybe not enough of Swift 4 is there yet for it to work, or maybe I am missing something obvious and need to go to sleep now — but it’s close enough to suggest the approach:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;public protocol TransformConvertible { // or whatever you want to call it  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;associatedtype From  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;associatedtype To  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var transform: (From) -\> To { get }  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;extension KeyPath: TransformConvertible {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public typealias From = Root  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public typealias To = Value  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public var transform: (Root) -\> Value {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return { $0[keypath: self] }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;extension Sequence {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public func map\<T, U\>(\_ transformSource: U) -\> [T]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where U: TransformConvertible,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U.From == Element,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U.To == T {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return map(transformSource.transform)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > This seems a bit more ambitious, perhaps not suitable for this round of Swift evolution work. But I throw it out there at least to show that supporting people.map(\.firstName) today would not preclude a generic keypath → function mechanism in the future:
> > 
> > A flavor of map that accepts a keypath today could be generalized to accept TransformConvertible in the future without breaking existing code.  
> > When calling a function that doesn’t know how to work with TransformConvertible, you could use (Foo.bar).transform, no special operator needed.
> > 
> > Cheers,
> > 
> > Paul
> > 
> > P.S. Largely irrelevant Ruby aside: Ruby’s & is not a free-floating operator, but part of the method invocation syntax indicating that the following arg should be treated as a block. Ruby calls a to\_proc method on whatever is in that position. Symbol implements to\_proc by returning a lambda that calls the method named by the symbol on the lambda’s first arg. Very much the duck-typed version of TransformConvertible above.
> > 
> > > On Jun 7, 2017, at 10:21 PM, Stephen Celis via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > 
> > > -1
> > > 
> > > A -1 from me may be surprising. I'm excited about key path composition and generic solutions, e.g. this experiment with lenses: [https://twitter.com/stephencelis/status/863916921577758721](https://twitter.com/stephencelis/status/863916921577758721)
> > > 
> > > But I'd prefer a reusable solution for converting key paths into functions.
> > > 
> > > Heaven help me for this Rubyism, but a prefix "&" operator (or, maybe better yet, some implicit mechanism) could convert a key-path to a function that passes a root value to a key path...
> > > 
> > > &nbsp;&nbsp;people.map(&\.firstName)
> > > 
> > > This way any function that takes a transformation from "whole" to "part" could take a key path. Requiring an overload per instance is less flexible.
> > > 
> > > Stephen
> > > 
> > > > On Jun 7, 2017, at 10:58 PM, Tony Allevato via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > > 
> > > > +1, I really like this. It would also align nicely with the method type flattening in SE-0042 (once it gets implemented), because passing keypaths (i.e., unbound property references) and unbound parameterless method references to map/flatMap would look nearly the same:
> > > > 
> > > > ```swift
> > > > struct Person {
> > > > let firstName: String
> > > > let lastName: String
> > > > func fullName() -> String { return "\(firstName) \(lastName)" }
> > > > }
> > > > 
> > > > let people: [Person]
> > > > let firstNames = people.map(\.firstName)
> > > > let fullNames = people.map(Person.fullName) // because after SE-0042, this will be (Person) -> String, not (Person) -> () -> String
> > > > 
> > > > ```
> > > > 
> > > > Especially if there's a move in the future to also use \. to denote unbound methods references, which was discussed during the keypath reviews. (Even with that, I believe it would be more work though to get rid of the explicit type name in the function case.)
> > > > 
> > > > On Wed, Jun 7, 2017 at 6:11 PM Xiaodi Wu via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:  
> > > > +1. Would think that all variants should exist on Optional too unless it would be harmful.  
> > > > On Wed, Jun 7, 2017 at 20:13 Michael J LeHew Jr via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:  
> > > > This is a great idea, and ought to be easy enough to bring forward! +1 from me!
> > > > 
> > > > -Michael
> > > > 
> > > > > On Jun 7, 2017, at 11:18 AM, Matt Diephouse via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > > > 
> > > > > 💯
> > > > > 
> > > > > > On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > > > > 
> > > > > > The new smart key path feature is really lovely, and feels like a great addition to Swift.
> > > > > > 
> > > > > > It seems like it might be straightforward to add overloads of `map` and `flatMap` to the standard library to make use of the new functionality:
> > > > > > 
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;let managers = flatOrganisation.managers  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;let allEmployees = Set(managers.flatMap(\.directReports))  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;let employeeNames = Set(allEmployees.map(\.name))
> > > > > > 
> > > > > > This feels like a really natural way of working with key paths in a functional style. It makes a lot of sense for collections, and possibly for Optional too (although as far as I can see optional chaining is more or less equivalent, and with more compact syntax).
> > > > > > 
> > > > > > I’m hoping that this might be low-hanging fruit that could be considered for the Swift 4 release. I’d be happy to have a go at writing a proposal if there’s interest!
> > > > > > 
> > > > > > –Adam
> > > > > > 
> > > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > > swift-evolution mailing list  
> > > > > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > > > 
> > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > swift-evolution mailing list  
> > > > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![stephencelis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stephencelis/32/2675_2.png) [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Post date:** [June 8, 2017, 4:49am UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/19 "2017-06-08T04:49:28Z")

</div>

:D

Great example for what I was trying to say. We can compose such a thing with existing code and not require an overload per.

> **···**
>
> > On Jun 8, 2017, at 12:34 AM, Susan Cheng \<susan.doggie@gmail.com\> wrote:
> > 
> > this work,
> > 
> > prefix operator \*
> > 
> > prefix func \*\<Root, Value\>(keyPath: KeyPath\<Root, Value\>) -\> (Root) -\> Value {  
> > &nbsp;&nbsp;&nbsp;&nbsp;return { $0[keyPath: keyPath] }  
> > }
> > 
> > ["Hello, World"].map(\*\String.count) // [12]
> > 
> > 2017-06-08 12:19 GMT+08:00 Paul Cantrell via swift-evolution \<swift-evolution@swift.org\>:  
> > It should be possible to achieve Ruby-like generality in Swift with a protocol for “thing that can converted to a transform function.” That wouldn’t need a special & operator.
> > 
> > Here’s a sketch. This sketch doesn’t compile — maybe not enough of Swift 4 is there yet for it to work, or maybe I am missing something obvious and need to go to sleep now — but it’s close enough to suggest the approach:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;public protocol TransformConvertible { // or whatever you want to call it  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;associatedtype From  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;associatedtype To  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var transform: (From) -\> To { get }  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;extension KeyPath: TransformConvertible {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public typealias From = Root  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public typealias To = Value  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public var transform: (Root) -\> Value {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return { $0[keypath: self] }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;extension Sequence {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public func map\<T, U\>(\_ transformSource: U) -\> [T]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;where U: TransformConvertible,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U.From == Element,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;U.To == T {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return map(transformSource.transform)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > This seems a bit more ambitious, perhaps not suitable for this round of Swift evolution work. But I throw it out there at least to show that supporting people.map(\.firstName) today would not preclude a generic keypath → function mechanism in the future:
> > 
> > &nbsp;&nbsp;• A flavor of map that accepts a keypath today could be generalized to accept TransformConvertible in the future without breaking existing code.  
> > &nbsp;&nbsp;• When calling a function that doesn’t know how to work with TransformConvertible, you could use (Foo.bar).transform, no special operator needed.
> > 
> > Cheers,
> > 
> > Paul
> > 
> > P.S. Largely irrelevant Ruby aside: Ruby’s & is not a free-floating operator, but part of the method invocation syntax indicating that the following arg should be treated as a block. Ruby calls a to\_proc method on whatever is in that position. Symbol implements to\_proc by returning a lambda that calls the method named by the symbol on the lambda’s first arg. Very much the duck-typed version of TransformConvertible above.
> > 
> > > On Jun 7, 2017, at 10:21 PM, Stephen Celis via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > 
> > > -1
> > > 
> > > A -1 from me may be surprising. I'm excited about key path composition and generic solutions, e.g. this experiment with lenses: [https://twitter.com/stephencelis/status/863916921577758721](https://twitter.com/stephencelis/status/863916921577758721)
> > > 
> > > But I'd prefer a reusable solution for converting key paths into functions.
> > > 
> > > Heaven help me for this Rubyism, but a prefix "&" operator (or, maybe better yet, some implicit mechanism) could convert a key-path to a function that passes a root value to a key path...
> > > 
> > > &nbsp;&nbsp;people.map(&\.firstName)
> > > 
> > > This way any function that takes a transformation from "whole" to "part" could take a key path. Requiring an overload per instance is less flexible.
> > > 
> > > Stephen
> > > 
> > > > On Jun 7, 2017, at 10:58 PM, Tony Allevato via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > > 
> > > > +1, I really like this. It would also align nicely with the method type flattening in SE-0042 (once it gets implemented), because passing keypaths (i.e., unbound property references) and unbound parameterless method references to map/flatMap would look nearly the same:
> > > > 
> > > > ```swift
> > > > struct Person {
> > > > let firstName: String
> > > > let lastName: String
> > > > func fullName() -> String { return "\(firstName) \(lastName)" }
> > > > }
> > > > 
> > > > let people: [Person]
> > > > let firstNames = people.map(\.firstName)
> > > > let fullNames = people.map(Person.fullName) // because after SE-0042, this will be (Person) -> String, not (Person) -> () -> String
> > > > 
> > > > ```
> > > > 
> > > > Especially if there's a move in the future to also use \. to denote unbound methods references, which was discussed during the keypath reviews. (Even with that, I believe it would be more work though to get rid of the explicit type name in the function case.)
> > > > 
> > > > On Wed, Jun 7, 2017 at 6:11 PM Xiaodi Wu via swift-evolution \<swift-evolution@swift.org\> wrote:  
> > > > +1. Would think that all variants should exist on Optional too unless it would be harmful.  
> > > > On Wed, Jun 7, 2017 at 20:13 Michael J LeHew Jr via swift-evolution \<swift-evolution@swift.org\> wrote:  
> > > > This is a great idea, and ought to be easy enough to bring forward! +1 from me!
> > > > 
> > > > -Michael
> > > > 
> > > > > On Jun 7, 2017, at 11:18 AM, Matt Diephouse via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > > > 
> > > > > 💯
> > > > > 
> > > > > > On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution \<swift-evolution@swift.org\> wrote:
> > > > > > 
> > > > > > The new smart key path feature is really lovely, and feels like a great addition to Swift.
> > > > > > 
> > > > > > It seems like it might be straightforward to add overloads of `map` and `flatMap` to the standard library to make use of the new functionality:
> > > > > > 
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;let managers = flatOrganisation.managers  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;let allEmployees = Set(managers.flatMap(\.directReports))  
> > > > > > &nbsp;&nbsp;&nbsp;&nbsp;let employeeNames = Set(allEmployees.map(\.name))
> > > > > > 
> > > > > > This feels like a really natural way of working with key paths in a functional style. It makes a lot of sense for collections, and possibly for Optional too (although as far as I can see optional chaining is more or less equivalent, and with more compact syntax).
> > > > > > 
> > > > > > I’m hoping that this might be low-hanging fruit that could be considered for the Swift 4 release. I’d be happy to have a go at writing a proposal if there’s interest!
> > > > > > 
> > > > > > –Adam
> > > > > > 
> > > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > > swift-evolution mailing list  
> > > > > > swift-evolution@swift.org  
> > > > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > > > 
> > > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > > swift-evolution mailing list  
> > > > > swift-evolution@swift.org  
> > > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > > 
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > > swift-evolution mailing list  
> > > > swift-evolution@swift.org  
> > > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [June 9, 2017, 11:51pm UTC](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073/20 "2017-06-09T23:51:28Z")

</div>

> > +1, I really like this. It would also align nicely with the method type flattening in SE-0042 \<[https://github.com/apple/swift-evolution/blob/master/proposals/0042-flatten-method-types.md&gt](https://github.com/apple/swift-evolution/blob/master/proposals/0042-flatten-method-types.md&gt); (once it gets implemented), because passing keypaths (i.e., unbound property references) and unbound parameterless method references to map/flatMap would look nearly the same:
> > 
> > ```swift
> > struct Person {
> > let firstName: String
> > let lastName: String
> > func fullName() -> String { return "\(firstName) \(lastName)" }
> > }
> > 
> > let people: [Person]
> > let firstNames = people.map(\.firstName)
> > let fullNames = people.map(Person.fullName) // because after SE-0042, this will be (Person) -> String, not (Person) -> () -> String
> > 
> > ```
> > 
> > Especially if there's a move in the future to also use \. to denote unbound methods references, which was discussed during the keypath reviews. (Even with that, I believe it would be more work though to get rid of the explicit type name in the function case.)
> 
> When it comes to unbound method references, personally, I would love to see us ditch currying in some future version of Swift and move to full-blown partial application instead. We would need variadic generics if we wanted to expose them as nicely-typed objects as we do with KeyPaths.
> 
> Anyway, I think what you want is something like this (where VirtualKeyPath is a custom subclass of KeyPath which is lazily-evaluated using a closure).
> 
> extension KeyPath where Value: Collection {  
> &nbsp;&nbsp;&nbsp;&nbsp;func map\<T\>(\_ descendent: KeyPath\<Value, T\>) -\> VirtualKeyPath\<Root, [T]\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return VirtualKeyPath\<Root, [T]\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(obj: Root) -\> [T] in obj[keypath: self].map { $0[keypath: descendent] }  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> }
> 
> \Department.people.map(\.fullName).characters.count // type: VirtualKeyPath\<Department, [Int]\>
> 
> Custom subclasses of KeyPath are not allowed, so you can’t actually do this. I don’t know, maybe it wouldn’t be much overhead to add the one, closure-based VirtualKeyPath — clearly the architecture is meant to be flexible. Maybe it’s better to wait until Swift 5 for that, though.
> 
> - Karl

Two corrections to myself:

1) It’s KeyPath\<Value.Element, T\>

2) You could implement VirtualKeyPath today, but you wouldn’t get the chaining syntax. You’d have to just keep map-ping it every time:

\Department.people.map(\.fullName).map(\.characters.count) // type: VirtualKeyPath\<Department, [Int]\>

- Karl

> **···**
>
> > On 10. Jun 2017, at 01:42, Karl Wagner \<razielim@gmail.com\> wrote:
> > 
> > > On 8. Jun 2017, at 04:58, Tony Allevato via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> 
> > > On Wed, Jun 7, 2017 at 6:11 PM Xiaodi Wu via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:  
> > > +1. Would think that all variants should exist on Optional too unless it would be harmful.  
> > > On Wed, Jun 7, 2017 at 20:13 Michael J LeHew Jr via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:  
> > > This is a great idea, and ought to be easy enough to bring forward! +1 from me!
> > > 
> > > -Michael
> > > 
> > > \> On Jun 7, 2017, at 11:18 AM, Matt Diephouse via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:  
> > > \>  
> > > \> 💯  
> > > \>  
> > > \>\> On Jun 7, 2017, at 10:35 AM, Adam Sharp via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:  
> > > \>\>  
> > > \>\> The new smart key path feature is really lovely, and feels like a great addition to Swift.  
> > > \>\>  
> > > \>\> It seems like it might be straightforward to add overloads of `map` and `flatMap` to the standard library to make use of the new functionality:  
> > > \>\>  
> > > \>\> let managers = flatOrganisation.managers  
> > > \>\> let allEmployees = Set(managers.flatMap(\.directReports))  
> > > \>\> let employeeNames = Set(allEmployees.map(\.name))  
> > > \>\>  
> > > \>\> This feels like a really natural way of working with key paths in a functional style. It makes a lot of sense for collections, and possibly for Optional too (although as far as I can see optional chaining is more or less equivalent, and with more compact syntax).  
> > > \>\>  
> > > \>\> I’m hoping that this might be low-hanging fruit that could be considered for the Swift 4 release. I’d be happy to have a go at writing a proposal if there’s interest!  
> > > \>\>  
> > > \>\> –Adam  
> > > \>\>  
> > > \>\> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > \>\> swift-evolution mailing list  
> > > \>\> swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > \>\> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > > \>  
> > > \> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > \> swift-evolution mailing list  
> > > \> swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > \> [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)
> > > 
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)  
> > > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > > swift-evolution mailing list  
> > > swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>  
> > > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

[Next page](https://forums.swift.org/t/pitch-support-for-map-and-flatmap-with-smart-key-paths/6073.md?page=2)
