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

Side by Side Diff: sdk/lib/collection/collections.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.collection; 5 part of dart.collection;
6 6
7 /** 7 /**
8 * The [Collections] class implements static methods useful when 8 * The [Collections] class implements static methods useful when
9 * writing a class that implements [Collection] and the [iterator] 9 * writing a class that implements [Collection] and the [iterator]
10 * method. 10 * method.
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 if (!it.moveNext()) { 63 if (!it.moveNext()) {
64 throw new StateError("No elements"); 64 throw new StateError("No elements");
65 } 65 }
66 dynamic result; 66 dynamic result;
67 do { 67 do {
68 result = it.current; 68 result = it.current;
69 } while(it.moveNext()); 69 } while(it.moveNext());
70 return result; 70 return result;
71 } 71 }
72 72
73 static dynamic min(Iterable iterable, [int compare(var a, var b)]) {
74 if (compare == null) compare = Comparable.compare;
75 Iterator it = iterable.iterator;
76 if (!it.moveNext()) {
77 return null;
78 }
79 var min = it.current;
80 while (it.moveNext()) {
81 if (compare(min, it.current) > 0) min = it.current;
82 }
83 return min;
84 }
85
86 static dynamic max(Iterable iterable, [int compare(var a, var b)]) {
87 if (compare == null) compare = Comparable.compare;
88 Iterator it = iterable.iterator;
89 if (!it.moveNext()) {
90 return null;
91 }
92 var max = it.current;
93 while (it.moveNext()) {
94 if (compare(max, it.current) < 0) max = it.current;
95 }
96 return max;
97 }
98
73 static dynamic single(Iterable iterable) { 99 static dynamic single(Iterable iterable) {
74 Iterator it = iterable.iterator; 100 Iterator it = iterable.iterator;
75 if (!it.moveNext()) throw new StateError("No elements"); 101 if (!it.moveNext()) throw new StateError("No elements");
76 dynamic result = it.current; 102 dynamic result = it.current;
77 if (it.moveNext()) throw new StateError("More than one element"); 103 if (it.moveNext()) throw new StateError("More than one element");
78 return result; 104 return result;
79 } 105 }
80 106
81 static dynamic firstMatching(Iterable iterable, 107 static dynamic firstMatching(Iterable iterable,
82 bool test(dynamic value), 108 bool test(dynamic value),
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 * Returns true if the specified collection contains the specified object 297 * Returns true if the specified collection contains the specified object
272 * reference. 298 * reference.
273 */ 299 */
274 static _containsRef(Collection c, Object ref) { 300 static _containsRef(Collection c, Object ref) {
275 for (var e in c) { 301 for (var e in c) {
276 if (identical(e, ref)) return true; 302 if (identical(e, ref)) return true;
277 } 303 }
278 return false; 304 return false;
279 } 305 }
280 } 306 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698