And how do you write a @noescape version with this function?<span></span><br><br>Dave Abrahams &lt;<a href="mailto:dabrahams@apple.com">dabrahams@apple.com</a>&gt; 於 2015年12月31日星期四 寫道:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I don’t understand that argument.  Obviously the function would be documented and there would be examples showing how to use it.  Why would it confuse people?<div><br></div><div>I think you’d need much stronger reasons to justify adding an unbounded set of overloads (is every algorithm that takes a comparison closure going to get one of these?) when we can handle the problem economically with a single function.<br><div><div><div><br><div>
<div>-Dave</div>
</div>
<br><div><blockquote type="cite"><div>On Dec 31, 2015, at 12:04 AM, Susan Cheng &lt;<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;susan.doggie@gmail.com&#39;);" target="_blank">susan.doggie@gmail.com</a>&gt; wrote:</div><br><div>It confuses people if provide a global function <font size="2"><span style="background-color:rgba(255,255,255,0)">byComparing in stdlib which&#39;s doing nothing alone<span></span>.</span></font><br><br>Dave Abrahams &lt;<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;dabrahams@apple.com&#39;);" target="_blank">dabrahams@apple.com</a>&gt; 於 2015年12月31日星期四 寫道:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Why add all those algorithms when you can write this</div><div><br></div><div><div><div><font face="Menlo">func byComparing&lt;T, U: Comparable&gt;(getComparisonKey: (T)-&gt;U) -&gt; (T, T) -&gt; Bool {</font></div><div><font face="Menlo">  return { getComparisonKey($0) &lt; getComparisonKey($1) }</font></div><div><font face="Menlo">}</font></div><div><font face="Menlo"><br></font></div><div><font face="Menlo">peoples.sort(byComparing { $<a href="http://0.name/" target="_blank">0.name</a> })</font></div><div><br></div><div>?</div><div><br></div>-Dave</div>
</div>
<br><div><blockquote type="cite"><div>On Dec 30, 2015, at 10:38 PM, Susan Cheng via swift-evolution &lt;<a>swift-evolution@swift.org</a>&gt; wrote:</div><br><div><div dir="ltr"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">Consider the follows:</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">struct</span> Person {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    <br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">var</span> name: <span style="color:rgb(112,61,170)">String</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">var</span> age: <span style="color:rgb(112,61,170)">Int</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> peoples = [<span style="color:rgb(79,129,135)">Person</span>(name: <span style="color:rgb(209,47,27)">&quot;Hawk&quot;</span>, age: <span style="color:rgb(39,42,216)">24</span>), <span style="color:rgb(79,129,135)">Person</span>(name: <span style="color:rgb(209,47,27)">&quot;Andrew&quot;</span>, age: <span style="color:rgb(39,42,216)">23</span>)]</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span> youngest = <span style="color:rgb(79,129,135)">peoples</span>.<span style="color:rgb(61,29,129)">minElement</span> { $0.<span style="color:rgb(79,129,135)">age</span> &lt; $1.<span style="color:rgb(79,129,135)">age</span> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(61,29,129)">print</span><span>(</span>youngest<span>?.</span>name<span>)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span>it&#39;s silly that we always have to write the code like </span><span>{ $0.some &lt; $1.</span><span>some</span><span> } or </span><span>{ </span><span>some</span><span>($0) &lt; </span><span>some</span><span>($1)</span><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">so, we should add those methods to stdlib:</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(187,44,162)">extension</span><span> </span>SequenceType<span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span>/// Returns the minimum element in `self` or `nil` if the sequence is empty.</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(0,132,0)">///</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span>/// - Complexity: O(`elements.count`).</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(0,132,0)">///</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span>    </span>@warn_unused_result</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">func</span> minElement&lt;R : <span style="color:rgb(112,61,170)">Comparable</span>&gt;(<span style="color:rgb(187,44,162)">@noescape</span> by: (Generator.Element) <span style="color:rgb(187,44,162)">throws</span> -&gt; R) <span style="color:rgb(187,44,162)">rethrows</span> -&gt; <span style="color:rgb(112,61,170)">Generator</span>.<span style="color:rgb(112,61,170)">Element</span>? {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(187,44,162)">try</span> <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(61,29,129)">minElement</span> { <span style="color:rgb(187,44,162)">try</span> by($0) <span style="color:rgb(61,29,129)">&lt;</span> by($1) }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span>/// Returns the maximum element in `self` or `nil` if the sequence is empty.</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(0,132,0)">///</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span>/// - Complexity: O(`elements.count`).</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(0,132,0)">///</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span>    </span>@warn_unused_result</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">func</span> maxElement&lt;R : <span style="color:rgb(112,61,170)">Comparable</span>&gt;(<span style="color:rgb(187,44,162)">@noescape</span> by: (Generator.Element) <span style="color:rgb(187,44,162)">throws</span> -&gt; R) <span style="color:rgb(187,44,162)">rethrows</span> -&gt; <span style="color:rgb(112,61,170)">Generator</span>.<span style="color:rgb(112,61,170)">Element</span>? {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(187,44,162)">try</span> <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(61,29,129)">maxElement</span> { <span style="color:rgb(187,44,162)">try</span> by($0) <span style="color:rgb(61,29,129)">&lt;</span> by($1) }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(187,44,162)">public</span><span> </span><span style="color:rgb(187,44,162)">extension</span><span> </span>MutableCollectionType<span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    <br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span>/// Return an `Array` containing the sorted elements of `source`.</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span>/// according to `by`.</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(0,132,0)">///</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span>/// The sorting algorithm is not stable (can change the relative order of</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span>/// elements that compare equal).</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span>    </span>@warn_unused_result(mutable_variant=&quot;sortInPlace&quot;)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span>    </span><span style="color:rgb(187,44,162)">func</span><span> sort&lt;R : </span>Comparable<span>&gt;(</span><span style="color:rgb(187,44,162)">@noescape</span><span> by: (</span>Generator<span>.</span>Element<span>) -&gt; </span>R<span>) -&gt; [</span>Generator<span>.</span>Element<span>] {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(61,29,129)">sort</span> { by($0) <span style="color:rgb(61,29,129)">&lt;</span> by($1) }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">extension</span> <span style="color:rgb(112,61,170)">MutableCollectionType</span> <span style="color:rgb(187,44,162)">where</span> <span style="color:rgb(187,44,162)">Self</span>.Index : RandomAccessIndexType {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    <br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span>/// Sort `self` in-place according to `by`.</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(0,132,0)">///</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span>/// The sorting algorithm is not stable (can change the relative order of</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span>    </span>/// elements that compare equal).</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">mutating</span> <span style="color:rgb(187,44,162)">func</span> sortInPlace&lt;R : <span style="color:rgb(112,61,170)">Comparable</span>&gt;(<span style="color:rgb(187,44,162)">@noescape</span> by: (<span style="color:rgb(112,61,170)">Generator</span>.<span style="color:rgb(112,61,170)">Element</span>) -&gt; <span style="color:rgb(112,61,170)">R</span>) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(61,29,129)">sortInPlace</span> { by($0) <span style="color:rgb(61,29,129)">&lt;</span> by($1) }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div><br></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=JfMPa-2F7wwZPzsZ3QKA8NjtONIYX4SjbWuUxtpfsTY2hlalHAlXXQWhHvthJG-2BHbU6XIVq65CJWZ-2F0CTirC6jnYzWxvt7Xc-2BMGls-2B5pihB9p-2FQwEKtYC4ag7sx7taELCdT1dRwRpgk6xGwiWEI0dkd1BBB8fXICs7BqciC0dFo4tBlefw1Ov6wlDL9odNZyWSjYYlPKLHKZXMDKF0P-2BmVsgAIne37-2FbKxF0IVKSbWfb8-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
_______________________________________________<br>swift-evolution mailing list<br><a>swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div><br></div></blockquote>
</div></blockquote></div><br></div></div></div></div></div></blockquote>