<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">+1 for the reduce version that Andrew presented, it has wider applicability.<div class=""><br class=""><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On 4 Jan 2016, at 10:42 AM, Andrew Bennett via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">As an alternative to minElement and maxElement, this could reduce the number of overloads and provide autocomplete:<div class=""><br class=""></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="color:rgb(187,44,162)" class="">extension</span><span style="" class=""> </span>SequenceType<span style="" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="" class=""> </span>@warn_unused_result<br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">func</span> reduce<C: <span style="color:rgb(112,61,170)" class="">Comparable</span>>(</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">@noescape</span> combine: (C, C) <span style="color:rgb(187,44,162)" class="">throws</span> -> <span style="color:rgb(112,61,170)" class="">Bool</span>,</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">@noescape</span> by key: Generator.Element -> C</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="" class=""> ) </span><span style="color:rgb(187,44,162)" class="">rethrows</span><span style="" class=""> -> </span>Generator<span style="" class="">.</span>Element<span style="" class="">?</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">var</span> generator = <span style="color:rgb(187,44,162)" class="">self</span>.<span style="color:rgb(61,29,129)" class="">generate</span>()</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">guard</span> <span style="color:rgb(187,44,162)" class="">let</span> first = generator.<span style="color:rgb(61,29,129)" class="">next</span>() <span style="color:rgb(187,44,162)" class="">else</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">return</span> <span style="color:rgb(187,44,162)" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">var</span> best = first, bestKey = key(first)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">while</span> <span style="color:rgb(187,44,162)" class="">let</span> element = generator.<span style="color:rgb(61,29,129)" class="">next</span>() {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">let</span> elementKey = key(element)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">if</span> <span style="color:rgb(187,44,162)" class="">try</span> !combine(bestKey, elementKey) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> bestKey = elementKey</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> best = element</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">return</span> best</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color:rgb(61,29,129)" class="">print</span>([<span style="color:rgb(209,47,27)" class="">"a"</span>,<span style="color:rgb(209,47,27)" class="">"ab"</span>,<span style="color:rgb(209,47,27)" class="">"abc"</span>].<span style="color:rgb(49,89,93)" class="">reduce</span>(<, by: { $0.<span style="color:rgb(112,61,170)" class="">characters</span>.<span style="color:rgb(112,61,170)" class="">count</span> })) <span style="color:rgb(0,132,0)" class="">// Optional("a")</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)" class="">
</p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color:rgb(61,29,129)" class="">print</span>([<span style="color:rgb(209,47,27)" class="">"a"</span>,<span style="color:rgb(209,47,27)" class="">"ab"</span>,<span style="color:rgb(209,47,27)" class="">"abc"</span>].<span style="color:rgb(49,89,93)" class="">reduce</span>(>, by: { $0.<span style="color:rgb(112,61,170)" class="">characters</span>.<span style="color:rgb(112,61,170)" class="">count</span> })) <span style="color:rgb(0,132,0)" class="">// Optional("abc")</span></div></blockquote><font color="#008400" face="Menlo" class=""><span style="font-size:11px" class=""><br class=""></span></font>The regular minElement, maxElement methods could have this alternative when you don't want "<span style="font-family:Menlo;font-size:11px" class="">by</span>":<div class=""><font color="#008400" face="Menlo" class=""><span style="font-size:11px" class=""><br class=""></span></font><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="color:rgb(187,44,162)" class="">extension</span><span style="" class=""> </span><span style="color:rgb(112,61,170)" class="">SequenceType</span><span style="" class=""> {</span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="" class=""> </span>@warn_unused_result</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">func</span> reduce(</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">@noescape</span> combine: (Generator.Element, Generator.Element) <span style="color:rgb(187,44,162)" class="">throws</span> -> <span style="color:rgb(112,61,170)" class="">Bool</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="" class=""> ) </span><span style="color:rgb(187,44,162)" class="">rethrows</span><span style="" class=""> -> </span>Generator<span style="" class="">.</span>Element<span style="" class="">?</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">var</span> generator = <span style="color:rgb(187,44,162)" class="">self</span>.<span style="color:rgb(61,29,129)" class="">generate</span>()</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">guard</span> <span style="color:rgb(187,44,162)" class="">let</span> first = generator.<span style="color:rgb(61,29,129)" class="">next</span>() <span style="color:rgb(187,44,162)" class="">else</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">return</span> <span style="color:rgb(187,44,162)" class="">nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">var</span> best = first</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">while</span> <span style="color:rgb(187,44,162)" class="">let</span> element = generator.<span style="color:rgb(61,29,129)" class="">next</span>() {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> best = <span style="color:rgb(187,44,162)" class="">try</span> combine(best, element) ? best : element</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">return</span> best</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="color:rgb(61,29,129)" class="">print</span><span style="" class="">([</span><span style="color:rgb(39,42,216)" class="">0</span><span style="" class="">,</span><span style="color:rgb(39,42,216)" class="">1</span><span style="" class="">,</span><span style="color:rgb(39,42,216)" class="">2</span><span style="" class="">,</span><span style="color:rgb(39,42,216)" class="">3</span><span style="" class="">].</span><span style="color:rgb(49,89,93)" class="">reduce</span><span style="" class="">(<)) </span>// Optional(0)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="color:rgb(61,29,129)" class="">print</span><span style="" class="">([</span><span style="color:rgb(39,42,216)" class="">0</span><span style="" class="">,</span><span style="color:rgb(39,42,216)" class="">1</span><span style="" class="">,</span><span style="color:rgb(39,42,216)" class="">2</span><span style="" class="">,</span><span style="color:rgb(39,42,216)" class="">3</span><span style="" class="">].</span><span style="color:rgb(49,89,93)" class="">reduce</span><span style="" class="">(>)) </span>// Optional(3)</div></blockquote><div class=""><br class=""></div></div><div class="">It may be more efficient to define this on <span style="font-family:Menlo;font-size:11px;color:rgb(112,61,170)" class="">CollectionType</span><span style="font-family:Menlo;font-size:11px" class=""> </span><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)" class="">where</span><span style="font-family:Menlo;font-size:11px" class=""> SubSequence.Generator.Element == Generator.Element</span>, using <span style="color:rgb(61,29,129);font-family:Menlo;font-size:11px" class="">.first</span> and <span style="font-family: Menlo; font-size: 11px;" class="">.</span><span style="color:rgb(61,29,129);font-family:Menlo;font-size:11px" class="">dropFirst</span><span style="font-family: Menlo; font-size: 11px;" class="">()</span> rather than <span style="font-family:Menlo;font-size:11px" class="">.</span><span style="font-family:Menlo;font-size:11px;color:rgb(61,29,129)" class="">generate</span><span style="font-family:Menlo;font-size:11px" class="">()</span>, but it's less flexible and this was enough to illustrate the alternative.<br class=""></div><div class=""><br class=""></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Fri, Jan 1, 2016 at 7:20 AM, Brent Royal-Gordon via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">> I don’t get the resistance to Dave’s solution? I think it works well and much more applicable.<br class="">
<br class="">
</span>I have two issues:<br class="">
<br class="">
1. It'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 class="">
<br class="">
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 class="">
<br class="">
array.map { ($0, sortKey($0)) }.sort { $0.1 < $1.1 }.map { $0.0 }<br class="">
<br class="">
Having said that, I think both of these concerns are relatively minor.<br class="">
<span class="HOEnZb"><font color="#888888" class=""><br class="">
--<br class="">
Brent Royal-Gordon<br class="">
Architechies<br class="">
</font></span><div class="HOEnZb"><div class="h5"><br class="">
_______________________________________________<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" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</div></div></blockquote></div><br class=""></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=Vm9j-2B2K6zLqxUFTO82XA8HV2TThDz5lA3-2F-2Fpeujw7DSWyarC65gY5ENHlyCrKPiQXRqpoOQLZkLhtqSPZnNF7KSJyZQx7UsJw1BLqgCX3o3l85lzOAfx3d3qectqonmJwzVO7kLtxB7Pqsz4AENDHnBbLzMtBYuEBeW6p6L7AKsUVSTotjOqai0NoWKvrwcF82A-2BU3G5A58fxfJUhRb-2FwTfjSIS69oPEEwHjr-2BgOBbw-3D" alt="" width="1" height="1" border="0" style="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;" class="">
_______________________________________________<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>