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

Unified Diff: sdk/lib/core/iterable.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: Address review comments. Fix T->E in Iterable. 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/core/iterable.dart
diff --git a/sdk/lib/core/iterable.dart b/sdk/lib/core/iterable.dart
index 0f846397285b2dfa8e3cd49d8d1b5fc6d48b1159..9128b05afbcc84ee45c1c6fb9120270128d0e85e 100644
--- a/sdk/lib/core/iterable.dart
+++ b/sdk/lib/core/iterable.dart
@@ -148,6 +148,52 @@ abstract class Iterable<E> {
}
/**
+ * Find the least element in the iterable.
+ *
+ * Returns null if the iterable is empty.
+ * Otherwise returns an element [:x:] of this [Iterable] so that
+ * [:x:] is not greater than [:y:] (that is, [:compare(x, y) <= 0:]) for all
+ * other elements [:y:] in the iterable.
+ *
+ * The [compare] function must be a proper [Comparator<T>]. If a function is
+ * not provided, [compare] defaults to [Comparable.compare].
+ */
+ E min([int compare(E a, E b)]) {
+ if (compare == null) compare = Comparable.compare;
+ Iterator it = iterator;
+ if (!it.moveNext()) return null;
+ E min = it.current;
+ while (it.moveNext) {
Sean Eagan 2013/01/02 16:35:13 it.moveNext()
+ E current = it.current;
+ if (compare(min, current) > 0) min = current;
+ }
+ return min;
+ }
+
+ /**
+ * Find the largest element in the iterable.
+ *
+ * Returns null if the iterable is empty.
+ * Otherwise returns an element [:x:] of this [Iterable] so that
+ * [:x:] is not smaller than [:y:] (that is, [:compare(x, y) >= 0:]) for all
+ * other elements [:y:] in the iterable.
+ *
+ * The [compare] function must be a proper [Comparator<T>]. If a function is
+ * not provided, [compare] defaults to [Comparable.compare].
+ */
+ E max([int compare(E a, E b)]) {
+ if (compare == null) compare = Comparable.compare;
+ Iterator it = iterator;
+ if (!it.moveNext()) return null;
+ E max = it.current;
+ while (it.moveNext) {
Sean Eagan 2013/01/02 16:35:13 it.moveNext()
+ E current = it.current;
+ if (compare(max, current) < 0) max = current;
+ }
+ return max;
+ }
+
+ /**
* Returns true if there is no element in this collection.
*/
bool get isEmpty => !iterator.moveNext();

Powered by Google App Engine
This is Rietveld 408576698