-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
minMax
operator on iterable and comparators.
- Loading branch information
Showing
7 changed files
with
124 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
extension MinMaxComparator<T> on Comparator<T> { | ||
/// Returns the minimum of the two arguments [a] and [b]. | ||
T min(T a, T b) => this(a, b) < 0 ? a : b; | ||
|
||
/// Returns the maximum of the provided [iterable]. | ||
T minOf(Iterable<T> iterable, {T Function()? orElse}) => | ||
orElse != null && iterable.isEmpty ? orElse() : iterable.reduce(min); | ||
|
||
/// Returns the maximum of the two arguments [a] and [b]. | ||
T max(T a, T b) => this(a, b) > 0 ? a : b; | ||
|
||
/// Returns the maximum of the provided [iterable]. | ||
T maxOf(Iterable<T> iterable, {T Function()? orElse}) => | ||
orElse != null && iterable.isEmpty ? orElse() : iterable.reduce(max); | ||
|
||
/// Returns a tuple with the minimum and maximum of the provided [iterable]. | ||
({T min, T max}) minMaxOf(Iterable<T> iterable, | ||
{({T min, T max}) Function()? orElse}) { | ||
final iterator = iterable.iterator; | ||
if (iterator.moveNext()) { | ||
var minValue = iterator.current; | ||
var maxValue = iterator.current; | ||
while (iterator.moveNext()) { | ||
minValue = min(minValue, iterator.current); | ||
maxValue = max(maxValue, iterator.current); | ||
} | ||
return (min: minValue, max: maxValue); | ||
} | ||
if (orElse == null) throw StateError("No element"); | ||
return orElse(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters