<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="">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 class=""><br class=""></div><div class="">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 class=""><div class=""><div class=""><div class=""><br class=""><div class="">
<div class="">-Dave</div>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Dec 31, 2015, at 12:04 AM, Susan Cheng <<a href="mailto:susan.doggie@gmail.com" class="">susan.doggie@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">It confuses people if provide a global function <font size="2" class=""><span style="background-color:rgba(255,255,255,0)" class="">byComparing in stdlib which's doing nothing alone<span class=""></span>.</span></font><br class=""><br class="">Dave Abrahams <<a href="mailto:dabrahams@apple.com" class="">dabrahams@apple.com</a>> 於 2015年12月31日星期四 寫道:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">Why add all those algorithms when you can write this</div><div class=""><br class=""></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 { $<a href="http://0.name/" target="_blank" class="">0.name</a> })</font></div><div class=""><br class=""></div><div class="">?</div><div class=""><br class=""></div>-Dave</div>
</div>
<br class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 30, 2015, at 10:38 PM, Susan Cheng via swift-evolution <<a href="javascript:_e(%7B%7D,'cvml','swift-evolution@swift.org');" target="_blank" class="">swift-evolution@swift.org</a>> wrote:</div><br class=""><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><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="">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 class="">(</span>youngest<span class="">?.</span>name<span class="">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)" class=""><span 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 class="">it's silly that we always have to write the code like </span><span class="">{ $0.some < $1.</span><span class="">some</span><span class=""> } or </span><span class="">{ </span><span class="">some</span><span class="">($0) < </span><span class="">some</span><span class="">($1)</span><span 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 class=""> </span>SequenceType<span class=""> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class=""><span 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 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 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 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 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 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 class=""> </span><span style="color:rgb(187,44,162)" class="">extension</span><span class=""> </span>MutableCollectionType<span class=""> {</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(0,132,0)" class=""><span 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 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 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 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 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 class=""> </span><span style="color:rgb(187,44,162)" class="">func</span><span class=""> sort<R : </span>Comparable<span class="">>(</span><span style="color:rgb(187,44,162)" class="">@noescape</span><span class=""> by: (</span>Generator<span class="">.</span>Element<span class="">) -> </span>R<span class="">) -> [</span>Generator<span class="">.</span>Element<span 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><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 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 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 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="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" class="">
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="javascript:_e(%7B%7D,'cvml','swift-evolution@swift.org');" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></blockquote>
</div></blockquote></div><br class=""></div></div></div></div></body></html>