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

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: 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..4a98650c18ffba6f629ca5d7b9493abbb96a7384 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].
+ */
+ T min([int compare(T a, T b)]) {
+ if (compare == null) compare = Comparable.compare;
+ Iterator it = iterator;
+ if (!it.moveNext()) return null;
+ T min = it.current;
+ while (it.moveNext) {
+ T 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].
+ */
+ T max([int compare(T a, T b)]) {
+ if (compare == null) compare = Comparable.compare;
+ Iterator it = iterator;
+ if (!it.moveNext()) return null;
+ T max = it.current;
+ while (it.moveNext) {
+ T 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