[swift-evolution] sortBy, minElementBy and maxElementBy methods

Dave Abrahams dabrahams at apple.com
Thu Dec 31 02:09:17 CST 2015


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?

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.

-Dave

> On Dec 31, 2015, at 12:04 AM, Susan Cheng <susan.doggie at gmail.com> wrote:
> 
> It confuses people if provide a global function byComparing in stdlib which's doing nothing alone.
> 
> Dave Abrahams <dabrahams at apple.com <mailto:dabrahams at apple.com>> 於 2015年12月31日星期四 寫道:
> Why add all those algorithms when you can write this
> 
> func byComparing<T, U: Comparable>(getComparisonKey: (T)->U) -> (T, T) -> Bool {
>   return { getComparisonKey($0) < getComparisonKey($1) }
> }
> 
> peoples.sort(byComparing { $0.name <http://0.name/> })
> 
> ?
> 
> -Dave
> 
>> On Dec 30, 2015, at 10:38 PM, Susan Cheng via swift-evolution <swift-evolution at swift.org <javascript:_e(%7B%7D,'cvml','swift-evolution at swift.org');>> wrote:
>> 
>> 
>> Consider the follows:
>> 
>> struct Person {
>>     
>>     var name: String
>>     var age: Int
>> }
>> 
>> let peoples = [Person(name: "Hawk", age: 24), Person(name: "Andrew", age: 23)]
>> 
>> let youngest = peoples.minElement { $0.age < $1.age }
>> 
>> print(youngest?.name)
>> 
>> it's silly that we always have to write the code like { $0.some < $1.some } or { some($0) < some($1) }
>> 
>> so, we should add those methods to stdlib:
>> 
>> extension SequenceType {
>>     /// Returns the minimum element in `self` or `nil` if the sequence is empty.
>>     ///
>>     /// - Complexity: O(`elements.count`).
>>     ///
>>     @warn_unused_result
>>     public func minElement<R : Comparable>(@noescape by: (Generator.Element) throws -> R) rethrows -> Generator.Element? {
>>         return try self.minElement { try by($0) < by($1) }
>>     }
>>     /// Returns the maximum element in `self` or `nil` if the sequence is empty.
>>     ///
>>     /// - Complexity: O(`elements.count`).
>>     ///
>>     @warn_unused_result
>>     public func maxElement<R : Comparable>(@noescape by: (Generator.Element) throws -> R) rethrows -> Generator.Element? {
>>         return try self.maxElement { try by($0) < by($1) }
>>     }
>> }
>> 
>> public extension MutableCollectionType {
>>     
>>     /// Return an `Array` containing the sorted elements of `source`.
>>     /// according to `by`.
>>     ///
>>     /// The sorting algorithm is not stable (can change the relative order of
>>     /// elements that compare equal).
>>     @warn_unused_result(mutable_variant="sortInPlace")
>>     func sort<R : Comparable>(@noescape by: (Generator.Element) -> R) -> [Generator.Element] {
>>         return self.sort { by($0) < by($1) }
>>     }
>> }
>> 
>> public extension MutableCollectionType where Self.Index : RandomAccessIndexType {
>>     
>>     /// Sort `self` in-place according to `by`.
>>     ///
>>     /// The sorting algorithm is not stable (can change the relative order of
>>     /// elements that compare equal).
>>     mutating func sortInPlace<R : Comparable>(@noescape by: (Generator.Element) -> R) {
>>         self.sortInPlace { by($0) < by($1) }
>>     }
>> }
>> 
>>  _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <javascript:_e(%7B%7D,'cvml','swift-evolution at swift.org');>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151231/5b15af17/attachment.html>


More information about the swift-evolution mailing list