Chromium Code Reviews| Index: sdk/lib/collection/collections.dart |
| diff --git a/sdk/lib/collection/collections.dart b/sdk/lib/collection/collections.dart |
| index 230cfaef786c8c4ac60e386e6d1214a3a447a136..4bd1fadb513ce5e84a36fbb51ae347e2db93af69 100644 |
| --- a/sdk/lib/collection/collections.dart |
| +++ b/sdk/lib/collection/collections.dart |
| @@ -70,6 +70,32 @@ class Collections { |
| return result; |
| } |
| + static dynamic min(Iterable iterable, [int compare(var a, var b)]) { |
| + if (compare == null) compare = Comparable.compare; |
| + Iterator it = iterable.iterator; |
| + if (!it.moveNext()) { |
| + return null; |
|
Anders Johnsen
2013/01/02 11:04:05
State error? [].min() -> null?
Lasse Reichstein Nielsen
2013/01/02 11:49:33
It's deliberate. We can argue whether it's the rig
|
| + } |
| + var min = it.current; |
| + while (it.moveNext()) { |
| + if (compare(min, it.current) > 0) min = it.current; |
| + } |
| + return min; |
| + } |
| + |
| + static dynamic max(Iterable iterable, [int compare(var a, var b)]) { |
| + if (compare == null) compare = Comparable.compare; |
| + Iterator it = iterable.iterator; |
| + if (!it.moveNext()) { |
| + return null; |
| + } |
| + var max = it.current; |
| + while (it.moveNext()) { |
| + if (compare(max, it.current) < 0) max = it.current; |
| + } |
| + return max; |
| + } |
| + |
| static dynamic single(Iterable iterable) { |
| Iterator it = iterable.iterator; |
| if (!it.moveNext()) throw new StateError("No elements"); |