[swift-evolution] [Pitch] KeyPath based map, flatMap, filter

Benjamin Herzog mail at benchr.me
Wed Jul 5 11:31:07 CDT 2017

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)


persons.flatMap { $0.name }

Link to pull request: https://github.com/apple/swift/pull/10760

Link to proposal draft:
Thanks in advance for your feedback!

Benjamin Herzog

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170705/e128c65d/attachment.html>

More information about the swift-evolution mailing list