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

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: 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..28d137ff16106e5e7d6798fcfdf0a97e5db420c5 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 emtpy, the result is [:null:].
Anders Johnsen 2013/01/02 11:04:05 empty
Lasse Reichstein Nielsen 2013/01/02 11:49:33 Done.
+ * Otherwise the result is an value from the stream that is not greater
Anders Johnsen 2013/01/02 11:04:05 a value
Lasse Reichstein Nielsen 2013/01/02 11:49:33 Done.
+ * 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: () {
Anders Johnsen 2013/01/02 11:04:05 Is it an error if there ain't any elements?
Lasse Reichstein Nielsen 2013/01/02 11:49:33 No, min returns null in that case, also in Iterabl
+ future._setValue(min);
+ },
+ unsubscribeOnError: true
+ );
+ }
+
+ /**
+ * Finds the least element in the stream.
Anders Johnsen 2013/01/02 11:04:05 Update comment, largest element.
Lasse Reichstein Nielsen 2013/01/02 11:49:33 Done.
+ *
+ * If the stream is emtpy, the result is [:null:].
Anders Johnsen 2013/01/02 11:04:05 Ditto
Lasse Reichstein Nielsen 2013/01/02 11:49:33 Done.
+ * Otherwise the result is an 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> 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