<div dir="ltr">+1, although I wonder if the method names should be distinct (such as minElementBy, sortBy, etc.)<div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 30, 2015 at 10:38 PM, Susan Cheng via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">Consider the follows:</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">struct</span> Person {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p><p 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></p><p 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></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p><p 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>)]</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p><p 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> }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p><p 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 style="color:rgb(0,0,0)">(</span>youngest<span style="color:rgb(0,0,0)">?.</span>name<span style="color:rgb(0,0,0)">)</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(0,0,0)"><br></span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(0,0,0)">it&#39;s silly that we always have to write the code like </span><span style="color:rgb(0,0,0)">{ $0.some &lt; $1.</span><span style="color:rgb(0,0,0)">some</span><span style="color:rgb(0,0,0)"> } or </span><span style="color:rgb(0,0,0)">{ </span><span style="color:rgb(0,0,0)">some</span><span style="color:rgb(0,0,0)">($0) &lt; </span><span style="color:rgb(0,0,0)">some</span><span style="color:rgb(0,0,0)">($1)</span><span style="color:rgb(0,0,0)"> }</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">so, we should add those methods to stdlib:</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p 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 style="color:rgb(0,0,0)"> </span>SequenceType<span style="color:rgb(0,0,0)"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>/// Returns the minimum element in `self` or `nil` if the sequence is empty.</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(0,132,0)">///</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>/// - Complexity: O(`elements.count`).</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(0,132,0)">///</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:rgb(0,0,0)">    </span>@warn_unused_result</p>
<p 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>? {</p>
<p 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) }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>/// Returns the maximum element in `self` or `nil` if the sequence is empty.</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(0,132,0)">///</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>/// - Complexity: O(`elements.count`).</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(0,132,0)">///</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:rgb(0,0,0)">    </span>@warn_unused_result</p>
<p 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>? {</p>
<p 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) }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p 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 style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">extension</span><span style="color:rgb(0,0,0)"> </span>MutableCollectionType<span style="color:rgb(0,0,0)"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>/// Return an `Array` containing the sorted elements of `source`.</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>/// according to `by`.</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(0,132,0)">///</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>/// The sorting algorithm is not stable (can change the relative order of</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>/// elements that compare equal).</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:rgb(0,0,0)">    </span>@warn_unused_result(mutable_variant=&quot;sortInPlace&quot;)</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(0,0,0)">    </span><span style="color:rgb(187,44,162)">func</span><span style="color:rgb(0,0,0)"> sort&lt;R : </span>Comparable<span style="color:rgb(0,0,0)">&gt;(</span><span style="color:rgb(187,44,162)">@noescape</span><span style="color:rgb(0,0,0)"> by: (</span>Generator<span style="color:rgb(0,0,0)">.</span>Element<span style="color:rgb(0,0,0)">) -&gt; </span>R<span style="color:rgb(0,0,0)">) -&gt; [</span>Generator<span style="color:rgb(0,0,0)">.</span>Element<span style="color:rgb(0,0,0)">] {</span></p>
<p 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) }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p 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 {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>/// Sort `self` in-place according to `by`.</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(0,132,0)">///</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>/// The sorting algorithm is not stable (can change the relative order of</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span>/// elements that compare equal).</p>
<p 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>) {</p>
<p 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) }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><div><br></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=P-2BsYbBZHRBuLDBJaL4DIKDNfkkjpROowTyRAObV11qwkV5zYe8XBO5Gu5BFqsTUP3l6G5OOzBSBl2y5fVYa3iTBlydvTnmkr8Mv88TpnbMaCke9JeE-2Bad0gkf-2BMnRHLPQS6NmDhNUoYAjkpVKmpzWywLsVgOpluC07B3jiLDbhdtlLTQBQIJRrXCCEVZXoH6voLF4dArudsc5thqQwC4m1ECFC7S57cVtngCW59bi5c-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>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div></div>