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

Unified Diff: sdk/lib/async/stream.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/async/stream.dart
diff --git a/sdk/lib/async/stream.dart b/sdk/lib/async/stream.dart
index dd1acfd053880108150627608e11c26e5feca88e..ac9aa44f57100d188513095e9d91567aaa58f2cc 100644
--- a/sdk/lib/async/stream.dart
+++ b/sdk/lib/async/stream.dart
@@ -269,6 +269,64 @@ abstract class Stream<T> {
return future;
}
+ /**
+ * Finds the least element in the stream.
+ *
+ * If the stream is empty, the result is [:null:].
+ * Otherwise the result is a value from the stream that is not greater
+ * than any other value from the stream (according to [compare], which must
+ * be a [Comparator]).
+ *
+ * If [compare] is omitted, it defaults to [Comparable.compare].
+ */
+ Future<T> min([int compare(T a, T b)]) {
+ _FutureImpl<T> future = new _FutureImpl<T>();
+ StreamSubscription subscription;
+ T min = null;
+ subscription = subscribe(
+ onData: (T value) {
+ min = value;
+ subscription.onData = (T value) {
+ if (compare(min, value) > 0) min = value;
+ };
+ },
+ onError: future.setError,
+ onDone: () {
+ future._setValue(min);
+ },
+ unsubscribeOnError: true
+ );
+ }
+
+ /**
+ * Finds the least element in the stream.
+ *
+ * If the stream is emtpy, the result is [:null:].
+ * Otherwise the result is an value from the stream that is not greater
Sean Eagan 2013/01/02 16:35:13 greater -> less ?
+ * than any other value from the stream (according to [compare], which must
+ * be a [Comparator]).
+ *
+ * If [compare] is omitted, it defaults to [Comparable.compare].
+ */
+ Future<T> max([int compare(T a, T b)]) {
+ _FutureImpl<T> future = new _FutureImpl<T>();
+ StreamSubscription subscription;
+ T max = null;
+ subscription = subscribe(
+ onData: (T value) {
+ max = value;
+ subscription.onData = (T value) {
+ if (compare(max, value) < 0) max = value;
+ };
+ },
+ onError: future.setError,
+ onDone: () {
+ future._setValue(max);
+ },
+ unsubscribeOnError: true
+ );
+ }
+
/** Reports whether this stream contains any elements. */
Future<bool> get isEmpty {
_FutureImpl<bool> future = new _FutureImpl<bool>();

Powered by Google App Engine
This is Rietveld 408576698