yes. Shouldn't have shorter names to replace the minElementBy, maxElementBy and sortInPlaceBy
minBy and maxBy? Jacob Bandes-Storch <[email protected]> 於 2015年12月31日星期四 寫道: > +1, although I wonder if the method names should be distinct (such as > minElementBy, sortBy, etc.) > > On Wed, Dec 30, 2015 at 10:38 PM, Susan Cheng via swift-evolution < > [email protected] > <javascript:_e(%7B%7D,'cvml','[email protected]');>> 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 >> [email protected] >> <javascript:_e(%7B%7D,'cvml','[email protected]');> >> https://lists.swift.org/mailman/listinfo/swift-evolution >> >> >
_______________________________________________ swift-evolution mailing list [email protected] https://lists.swift.org/mailman/listinfo/swift-evolution
