Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(248)

Unified Diff: sdk/lib/collection/collections.dart

Issue 11727007: Add min and max to Iterable and Stream. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Created 7 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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");

Powered by Google App Engine
This is Rietveld 408576698