<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Considering these observations and more from Brent and Dave, I support ultimately making&nbsp;<span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(112, 61, 170);" class="">KeyPath</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">&lt;</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(79, 129, 135);" class="">T</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">,</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(79, 129, 135);" class="">U</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">&gt;) a</span>&nbsp;subtype of&nbsp;<span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">(</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(79, 129, 135);" class="">T</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255);" class="">)-&gt;</span><span style="font-family: Menlo; font-size: 11px; background-color: rgb(255, 255, 255); color: rgb(79, 129, 135);" class="">U&nbsp;</span>as suggested.<div class=""><br class=""></div><div class="">If someone really wants this now, I would go with a prefix operator as a stopgap:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class="">prefix<span style="color: #000000" class=""> </span>operator<span style="color: #000000" class=""> ~</span></div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">prefix</span> <span style="color: #ba2da2" class="">func</span> ~&lt;T,U&gt;(lhs: <span style="color: #703daa" class="">KeyPath</span>&lt;<span style="color: #4f8187" class="">T</span>,<span style="color: #4f8187" class="">U</span>&gt;) -&gt; (<span style="color: #4f8187" class="">T</span>)-&gt;<span style="color: #4f8187" class="">U</span> { <span style="color: #ba2da2" class="">return</span> { $0[keyPath: lhs] } }</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">struct</span> Guy { <span style="color: #ba2da2" class="">let</span> name: <span style="color: #703daa" class="">String</span> }</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">let</span> guys = [</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #4f8187" class="">Guy</span>(name: <span style="color: #d12f1b" class="">"Benjamin"</span>),</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #4f8187" class="">Guy</span>(name: <span style="color: #d12f1b" class="">"Dave"</span>),</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #4f8187" class="">Guy</span>(name: <span style="color: #d12f1b" class="">"Brent"</span>),</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">&nbsp; &nbsp; <span style="color: #4f8187" class="">Guy</span>(name: <span style="color: #d12f1b" class="">"Max"</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class="">]</div><div style="margin: 0px; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: #4f8187" class="">guys</span>.<span style="color: #3e1e81" class="">map</span>(~\.name)</div></div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 7, 2017, at 11:15 AM, Max Moiseev via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=us-ascii" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">A few more observations not necessarily supporting either side:<div class=""><br class=""><div class="">- Why only map, flatMap, and filter? drop, prefix, and sorted can also benefit from keyPaths.</div><div class="">- 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.</div><div class="">- 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...</div><div class=""><br class=""></div><div class="">Max<br class=""><div class=""><blockquote type="cite" class=""><div class="">On Jul 5, 2017, at 10:08 AM, Benjamin Herzog via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">


<title class=""></title>

<div class=""><div class="">Hey guys,<br class=""></div>
<div class=""><br class=""></div>
<div class="">I would like to pitch a small convenient change to the Swift stdlib. With KeyPaths added in&nbsp;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.<br class=""></div>
<div class="">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.<br class=""></div>
<div class="">I propose using&nbsp;<br class=""></div>
<div class=""><br class=""></div>
<div class="">persons.flatMap(keyPath: \.name)<br class=""></div>
<div class=""><br class=""></div>
<div class="">over<br class=""></div>
<div class=""><br class=""></div>
<div class="">persons.flatMap { $0.name }<br class=""></div>
<div class=""><br class=""></div>
<div class="">Link to pull request:&nbsp;<a href="https://github.com/apple/swift/pull/10760" class="">https://github.com/apple/swift/pull/10760</a><br class=""></div>
<div class=""><br class=""></div>
<div class="">Link to proposal draft:&nbsp;<a href="https://github.com/BenchR267/swift-evolution/blob/keypath-based-map/proposals/0181-keypath-based-map-flatmap-filter.md" class="">https://github.com/BenchR267/swift-evolution/blob/keypath-based-map/proposals/0181-keypath-based-map-flatmap-filter.md</a><br class=""></div>
<div class=""><br class=""></div>
<div class="">Thanks in advance for your feedback!<br class=""></div>
<div id="sig64302232" class=""><div class="signature">______________________<br class=""></div>
<div class="signature"><br class=""></div>
<div class="signature">Benjamin Herzog<br class=""></div>
</div>
<div class=""><br class=""></div>
</div>

_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>