[swift-evolution] [Pitch] KeyPath based map, flatMap, filter
Hooman Mehr
hooman at mac.com
Fri Jul 7 17:03:04 CDT 2017
Considering these observations and more from Brent and Dave, I support ultimately making KeyPath<T,U>) a subtype of (T)->U as suggested.
If someone really wants this now, I would go with a prefix operator as a stopgap:
prefix operator ~
prefix func ~<T,U>(lhs: KeyPath<T,U>) -> (T)->U { return { $0[keyPath: lhs] } }
struct Guy { let name: String }
let guys = [
Guy(name: "Benjamin"),
Guy(name: "Dave"),
Guy(name: "Brent"),
Guy(name: "Max")
]
guys.map(~\.name)
> On Jul 7, 2017, at 11:15 AM, Max Moiseev via swift-evolution <swift-evolution at swift.org> wrote:
>
> A few more observations not necessarily supporting either side:
>
> - Why only map, flatMap, and filter? drop, prefix, and sorted can also benefit from keyPaths.
> - Adding these overloads only to Sequence will have a very interesting side effect on code like: xs.lazy.map(\.name), because lazy. Therefore all these overloads will have to be duplicated everywhere.
> - Naive implementation (the one that simply calls the closure taking overload) was 4 times slower when I quickly tested it. On the positive side though, this same naive implementation does not have to be a part of standard library and can easily be provided by a third-party package...
>
> Max
>> On Jul 5, 2017, at 10:08 AM, Benjamin Herzog via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>
>> Hey guys,
>>
>> I would like to pitch a small convenient change to the Swift stdlib. With KeyPaths added in SE-0161 I would like to add some convenience calls to map, flatMap and filter in Sequences. To extract properties of an array of objects we currently use trailing closure syntax together with the shorthand $0 for the first closure argument. This is still kind of verbose and also hard to read in some situations.
>> I think it is much better to understand what is going on when using the type safe KeyPaths for that. I already implemented a working solution and would like to pitch the idea here to get some feedback before opening the swift evolution proposal.
>> I propose using
>>
>> persons.flatMap(keyPath: \.name)
>>
>> over
>>
>> persons.flatMap { $0.name }
>>
>> Link to pull request: https://github.com/apple/swift/pull/10760 <https://github.com/apple/swift/pull/10760>
>>
>> Link to proposal draft: https://github.com/BenchR267/swift-evolution/blob/keypath-based-map/proposals/0181-keypath-based-map-flatmap-filter.md <https://github.com/BenchR267/swift-evolution/blob/keypath-based-map/proposals/0181-keypath-based-map-flatmap-filter.md>
>>
>> Thanks in advance for your feedback!
>> ______________________
>>
>> Benjamin Herzog
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170707/01d787e2/attachment.html>
More information about the swift-evolution
mailing list