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

Side by Side 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, 11 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 // part of dart.async; 5 // part of dart.async;
6 6
7 // ------------------------------------------------------------------- 7 // -------------------------------------------------------------------
8 // Core Stream types 8 // Core Stream types
9 // ------------------------------------------------------------------- 9 // -------------------------------------------------------------------
10 10
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 subscribe( 262 subscribe(
263 onData: (_) { count++; }, 263 onData: (_) { count++; },
264 onError: future._setError, 264 onError: future._setError,
265 onDone: () { 265 onDone: () {
266 future._setValue(count); 266 future._setValue(count);
267 }, 267 },
268 unsubscribeOnError: true); 268 unsubscribeOnError: true);
269 return future; 269 return future;
270 } 270 }
271 271
272 /**
273 * Finds the least element in the stream.
274 *
275 * If the stream is empty, the result is [:null:].
276 * Otherwise the result is a value from the stream that is not greater
277 * than any other value from the stream (according to [compare], which must
278 * be a [Comparator]).
279 *
280 * If [compare] is omitted, it defaults to [Comparable.compare].
281 */
282 Future<T> min([int compare(T a, T b)]) {
283 _FutureImpl<T> future = new _FutureImpl<T>();
284 StreamSubscription subscription;
285 T min = null;
286 subscription = subscribe(
287 onData: (T value) {
288 min = value;
289 subscription.onData = (T value) {
290 if (compare(min, value) > 0) min = value;
291 };
292 },
293 onError: future.setError,
294 onDone: () {
295 future._setValue(min);
296 },
297 unsubscribeOnError: true
298 );
299 }
300
301 /**
302 * Finds the least element in the stream.
303 *
304 * If the stream is emtpy, the result is [:null:].
305 * Otherwise the result is an value from the stream that is not greater
Sean Eagan 2013/01/02 16:35:13 greater -> less ?
306 * than any other value from the stream (according to [compare], which must
307 * be a [Comparator]).
308 *
309 * If [compare] is omitted, it defaults to [Comparable.compare].
310 */
311 Future<T> max([int compare(T a, T b)]) {
312 _FutureImpl<T> future = new _FutureImpl<T>();
313 StreamSubscription subscription;
314 T max = null;
315 subscription = subscribe(
316 onData: (T value) {
317 max = value;
318 subscription.onData = (T value) {
319 if (compare(max, value) < 0) max = value;
320 };
321 },
322 onError: future.setError,
323 onDone: () {
324 future._setValue(max);
325 },
326 unsubscribeOnError: true
327 );
328 }
329
272 /** Reports whether this stream contains any elements. */ 330 /** Reports whether this stream contains any elements. */
273 Future<bool> get isEmpty { 331 Future<bool> get isEmpty {
274 _FutureImpl<bool> future = new _FutureImpl<bool>(); 332 _FutureImpl<bool> future = new _FutureImpl<bool>();
275 StreamSubscription subscription; 333 StreamSubscription subscription;
276 subscription = subscribe( 334 subscription = subscribe(
277 onData: (_) { 335 onData: (_) {
278 subscription.unsubscribe(); 336 subscription.unsubscribe();
279 future._setValue(false); 337 future._setValue(false);
280 }, 338 },
281 onError: future._setError, 339 onError: future._setError,
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after
783 sink.signalError(error); 841 sink.signalError(error);
784 } 842 }
785 843
786 /** 844 /**
787 * Handle an incoming done event. 845 * Handle an incoming done event.
788 */ 846 */
789 void handleDone(StreamSink<T> sink) { 847 void handleDone(StreamSink<T> sink) {
790 sink.close(); 848 sink.close();
791 } 849 }
792 } 850 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698