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(); |