<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=""><div class="">Why add all those algorithms when you can write this</div><div class=""><br class="webkit-block-placeholder"></div><div class=""><div class=""><div class=""><font face="Menlo" class="">func byComparing<T, U: Comparable>(getComparisonKey: (T)->U) -> (T, T) -> Bool {</font></div><div class=""><font face="Menlo" class=""> return { getComparisonKey($0) < getComparisonKey($1) }</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">peoples.sort(byComparing { $0.name })</font></div><div class=""><br class=""></div><div class="">?</div><div class=""><br class=""></div>-Dave</div>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Dec 30, 2015, at 10:38 PM, Susan Cheng 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=""><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; min-height: 13px;" class="">Consider the follows:</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(187,44,162)" class="">struct</span> Person {</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">var</span> name: <span style="color:rgb(112,61,170)" class="">String</span></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> age: <span style="color:rgb(112,61,170)" class="">Int</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; 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(187,44,162)" class="">let</span> peoples = [<span style="color:rgb(79,129,135)" class="">Person</span>(name: <span style="color:rgb(209,47,27)" class="">"Hawk"</span>, age: <span style="color:rgb(39,42,216)" class="">24</span>), <span style="color:rgb(79,129,135)" class="">Person</span>(name: <span style="color:rgb(209,47,27)" class="">"Andrew"</span>, age: <span style="color:rgb(39,42,216)" class="">23</span>)]</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(187,44,162)" class="">let</span> youngest = <span style="color:rgb(79,129,135)" class="">peoples</span>.<span style="color:rgb(61,29,129)" class="">minElement</span> { $0.<span style="color:rgb(79,129,135)" class="">age</span> < $1.<span style="color:rgb(79,129,135)" class="">age</span> }</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(79, 129, 135);" class=""><span style="color:rgb(61,29,129)" class="">print</span><span style="" class="">(</span>youngest<span style="" class="">?.</span>name<span style="" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="" class="">it's silly that we always have to write the code like </span><span style="" class="">{ $0.some < $1.</span><span style="" class="">some</span><span style="" class=""> } or </span><span style="" class="">{ </span><span style="" class="">some</span><span style="" class="">($0) < </span><span style="" class="">some</span><span style="" class="">($1)</span><span style="" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">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;" class=""><br class=""></div><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(0, 132, 0);" class=""><span style="" class=""> </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;" class=""> <span style="color:rgb(0,132,0)" class="">///</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="" class=""> </span>/// - Complexity: O(`elements.count`).</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(0,132,0)" 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</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">public</span> <span style="color:rgb(187,44,162)" class="">func</span> minElement<R : <span style="color:rgb(112,61,170)" class="">Comparable</span>>(<span style="color:rgb(187,44,162)" class="">@noescape</span> by: (Generator.Element) <span style="color:rgb(187,44,162)" class="">throws</span> -> R) <span style="color:rgb(187,44,162)" class="">rethrows</span> -> <span style="color:rgb(112,61,170)" class="">Generator</span>.<span style="color:rgb(112,61,170)" class="">Element</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="">try</span> <span style="color:rgb(187,44,162)" class="">self</span>.<span style="color:rgb(61,29,129)" class="">minElement</span> { <span style="color:rgb(187,44,162)" class="">try</span> by($0) <span style="color:rgb(61,29,129)" class=""><</span> by($1) }</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; color: rgb(0, 132, 0);" class=""><span style="" class=""> </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;" class=""> <span style="color:rgb(0,132,0)" class="">///</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="" class=""> </span>/// - Complexity: O(`elements.count`).</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(0,132,0)" 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</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">public</span> <span style="color:rgb(187,44,162)" class="">func</span> maxElement<R : <span style="color:rgb(112,61,170)" class="">Comparable</span>>(<span style="color:rgb(187,44,162)" class="">@noescape</span> by: (Generator.Element) <span style="color:rgb(187,44,162)" class="">throws</span> -> R) <span style="color:rgb(187,44,162)" class="">rethrows</span> -> <span style="color:rgb(112,61,170)" class="">Generator</span>.<span style="color:rgb(112,61,170)" class="">Element</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="">try</span> <span style="color:rgb(187,44,162)" class="">self</span>.<span style="color:rgb(61,29,129)" class="">maxElement</span> { <span style="color:rgb(187,44,162)" class="">try</span> by($0) <span style="color:rgb(61,29,129)" class=""><</span> by($1) }</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(112, 61, 170);" class=""><span style="color:rgb(187,44,162)" class="">public</span><span style="" class=""> </span><span style="color:rgb(187,44,162)" class="">extension</span><span style="" class=""> </span>MutableCollectionType<span style="" class=""> {</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="" class=""> </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);" class=""><span style="" class=""> </span>/// according to `by`.</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(0,132,0)" class="">///</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="" class=""> </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);" class=""><span style="" class=""> </span>/// elements that compare equal).</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(mutable_variant="sortInPlace")</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="">func</span><span style="" class=""> sort<R : </span>Comparable<span style="" class="">>(</span><span style="color:rgb(187,44,162)" class="">@noescape</span><span style="" class=""> by: (</span>Generator<span style="" class="">.</span>Element<span style="" class="">) -> </span>R<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=""> <span style="color:rgb(187,44,162)" class="">return</span> <span style="color:rgb(187,44,162)" class="">self</span>.<span style="color:rgb(61,29,129)" class="">sort</span> { by($0) <span style="color:rgb(61,29,129)" class=""><</span> by($1) }</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(187,44,162)" class="">public</span> <span style="color:rgb(187,44,162)" class="">extension</span> <span style="color:rgb(112,61,170)" class="">MutableCollectionType</span> <span style="color:rgb(187,44,162)" class="">where</span> <span style="color:rgb(187,44,162)" class="">Self</span>.Index : RandomAccessIndexType {</div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class=""> <br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="" class=""> </span>/// Sort `self` in-place according to `by`.</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(0,132,0)" class="">///</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="" class=""> </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);" class=""><span style="" class=""> </span>/// elements that compare equal).</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">mutating</span> <span style="color:rgb(187,44,162)" class="">func</span> sortInPlace<R : <span style="color:rgb(112,61,170)" class="">Comparable</span>>(<span style="color:rgb(187,44,162)" class="">@noescape</span> by: (<span style="color:rgb(112,61,170)" class="">Generator</span>.<span style="color:rgb(112,61,170)" class="">Element</span>) -> <span style="color:rgb(112,61,170)" class="">R</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">self</span>.<span style="color:rgb(61,29,129)" class="">sortInPlace</span> { by($0) <span style="color:rgb(61,29,129)" class=""><</span> by($1) }</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 class=""><br class=""></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="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=""></body></html>