Skip to content

Commit

Permalink
Merge pull request #136 from dn-m/min-max-by-property
Browse files Browse the repository at this point in the history
Add min and max extrema by property
  • Loading branch information
jsbean authored Aug 11, 2018
2 parents d20525b + ad6f5ec commit 08397f8
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Sources/DataStructures/SequenceExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ extension Sequence {
guard let extremum = sorted.first.flatMap(property) else { return [] }
return sorted.filter { property($0) == extremum }
}

/// - Returns: All of the values which are equivalent to the minimum value contained herein.
public func min <T> (property: (Element) -> T) -> [Element] where T: Comparable {
return extrema(property: property, areInIncreasingOrder: <)
}

/// - Returns: All of the values which are equivalent to the maximum value contained herein.
public func max <T> (property: (Element) -> T) -> [Element] where T: Comparable {
return extrema(property: property, areInIncreasingOrder: >)
}
}

/// - Returns: `true` if the given `array` contains the given `value`.
Expand Down

0 comments on commit 08397f8

Please sign in to comment.