<div dir="ltr">As an alternative to minElement and maxElement, this could reduce the number of overloads and provide autocomplete:<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><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(187,44,162)"><span style="color:rgb(0,0,0)">    </span>@warn_unused_result<br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">func</span> reduce&lt;C: <span style="color:rgb(112,61,170)">Comparable</span>&gt;(</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">@noescape</span> combine: (C, C) <span style="color:rgb(187,44,162)">throws</span> -&gt; <span style="color:rgb(112,61,170)">Bool</span>,</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">@noescape</span> by key: Generator.Element -&gt; C</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)">rethrows</span><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">    {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">var</span> generator = <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(61,29,129)">generate</span>()</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">guard</span> <span style="color:rgb(187,44,162)">let</span> first = generator.<span style="color:rgb(61,29,129)">next</span>() <span style="color:rgb(187,44,162)">else</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)">nil</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">        <span style="color:rgb(187,44,162)">var</span> best = first, bestKey = key(first)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">while</span> <span style="color:rgb(187,44,162)">let</span> element = generator.<span style="color:rgb(61,29,129)">next</span>() {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            <span style="color:rgb(187,44,162)">let</span> elementKey = key(element)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            <span style="color:rgb(187,44,162)">if</span> <span style="color:rgb(187,44,162)">try</span> !combine(bestKey, elementKey) {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">                bestKey = elementKey</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">                best = element</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">        <span style="color:rgb(187,44,162)">return</span> best</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(61,29,129)">print</span>([<span style="color:rgb(209,47,27)">&quot;a&quot;</span>,<span style="color:rgb(209,47,27)">&quot;ab&quot;</span>,<span style="color:rgb(209,47,27)">&quot;abc&quot;</span>].<span style="color:rgb(49,89,93)">reduce</span>(&lt;, by: { $0.<span style="color:rgb(112,61,170)">characters</span>.<span style="color:rgb(112,61,170)">count</span> })) <span style="color:rgb(0,132,0)">// Optional(&quot;a&quot;)</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)">












































</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(61,29,129)">print</span>([<span style="color:rgb(209,47,27)">&quot;a&quot;</span>,<span style="color:rgb(209,47,27)">&quot;ab&quot;</span>,<span style="color:rgb(209,47,27)">&quot;abc&quot;</span>].<span style="color:rgb(49,89,93)">reduce</span>(&gt;, by: { $0.<span style="color:rgb(112,61,170)">characters</span>.<span style="color:rgb(112,61,170)">count</span> })) <span style="color:rgb(0,132,0)">// Optional(&quot;abc&quot;)</span></p></blockquote><font color="#008400" face="Menlo"><span style="font-size:11px"><br></span></font>The regular minElement, maxElement methods could have this alternative when you don&#39;t want &quot;<span style="font-family:Menlo;font-size:11px">by</span>&quot;:<div><font color="#008400" face="Menlo"><span style="font-size:11px"><br></span></font><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="color:rgb(187,44,162)">extension</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(112,61,170)">SequenceType</span><span style="color:rgb(0,0,0)"> {</span><br></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)">func</span> reduce(</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">@noescape</span> combine: (Generator.Element, Generator.Element) <span style="color:rgb(187,44,162)">throws</span> -&gt; <span style="color:rgb(112,61,170)">Bool</span></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)">rethrows</span><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">    {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">var</span> generator = <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(61,29,129)">generate</span>()</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">guard</span> <span style="color:rgb(187,44,162)">let</span> first = generator.<span style="color:rgb(61,29,129)">next</span>() <span style="color:rgb(187,44,162)">else</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)">nil</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">        <span style="color:rgb(187,44,162)">var</span> best = first</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">while</span> <span style="color:rgb(187,44,162)">let</span> element = generator.<span style="color:rgb(61,29,129)">next</span>() {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            best = <span style="color:rgb(187,44,162)">try</span> combine(best, element) ? best : element</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">        <span style="color:rgb(187,44,162)">return</span> best</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(0,132,0)"><span style="color:rgb(61,29,129)">print</span><span style="color:rgb(0,0,0)">([</span><span style="color:rgb(39,42,216)">0</span><span style="color:rgb(0,0,0)">,</span><span style="color:rgb(39,42,216)">1</span><span style="color:rgb(0,0,0)">,</span><span style="color:rgb(39,42,216)">2</span><span style="color:rgb(0,0,0)">,</span><span style="color:rgb(39,42,216)">3</span><span style="color:rgb(0,0,0)">].</span><span style="color:rgb(49,89,93)">reduce</span><span style="color:rgb(0,0,0)">(&lt;)) </span>// Optional(0)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(61,29,129)">print</span><span style="color:rgb(0,0,0)">([</span><span style="color:rgb(39,42,216)">0</span><span style="color:rgb(0,0,0)">,</span><span style="color:rgb(39,42,216)">1</span><span style="color:rgb(0,0,0)">,</span><span style="color:rgb(39,42,216)">2</span><span style="color:rgb(0,0,0)">,</span><span style="color:rgb(39,42,216)">3</span><span style="color:rgb(0,0,0)">].</span><span style="color:rgb(49,89,93)">reduce</span><span style="color:rgb(0,0,0)">(&gt;)) </span>// Optional(3)</p></blockquote><div><br></div></div><div>It may be more efficient to define this on <span style="font-family:Menlo;font-size:11px;color:rgb(112,61,170)">CollectionType</span><span style="font-family:Menlo;font-size:11px"> </span><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">where</span><span style="font-family:Menlo;font-size:11px"> SubSequence.Generator.Element == Generator.Element</span>, using <span style="color:rgb(61,29,129);font-family:Menlo;font-size:11px">.first</span> and <span style="font-family:Menlo;font-size:11px;color:rgb(0,0,0)">.</span><span style="color:rgb(61,29,129);font-family:Menlo;font-size:11px">dropFirst</span><span style="font-family:Menlo;font-size:11px;color:rgb(0,0,0)">()</span> rather than <span style="font-family:Menlo;font-size:11px">.</span><span style="font-family:Menlo;font-size:11px;color:rgb(61,29,129)">generate</span><span style="font-family:Menlo;font-size:11px">()</span>, but it&#39;s less flexible and this was enough to illustrate the alternative.<br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 1, 2016 at 7:20 AM, Brent Royal-Gordon 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"><span class="">&gt; I don’t get the resistance to Dave’s solution? I think it works well and much more applicable.<br>
<br>
</span>I have two issues:<br>
<br>
1. It&#39;s less discoverable. Someone merely *typing* `sort` into their editor will see `sortBy` right below it in their autocompletion list, and might discover it that way.<br>
<br>
2. It forces a naïve implementation, which may not be the best idea. In the Perl world, for instance, we would usually use a Schwartzian transform to implement this, particularly if the key might be expensive to compute:<br>
<br>
        array.map { ($0, sortKey($0)) }.sort { $0.1 &lt; $1.1 }.map { $0.0 }<br>
<br>
Having said that, I think both of these concerns are relatively  minor.<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Brent Royal-Gordon<br>
Architechies<br>
</font></span><div class="HOEnZb"><div class="h5"><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>
</div></div></blockquote></div><br></div>