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

Side by Side Diff: sdk/lib/core/iterable.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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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.core; 5 part of dart.core;
6 6
7 /** 7 /**
8 * The [Iterable] interface allows to get an [Iterator] out of an 8 * The [Iterable] interface allows to get an [Iterator] out of an
9 * [Iterable] object. 9 * [Iterable] object.
10 * 10 *
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 int get length { 141 int get length {
142 int count = 0; 142 int count = 0;
143 Iterator it = iterator; 143 Iterator it = iterator;
144 while (it.moveNext()) { 144 while (it.moveNext()) {
145 count++; 145 count++;
146 } 146 }
147 return count; 147 return count;
148 } 148 }
149 149
150 /** 150 /**
151 * Find the least element in the iterable.
152 *
153 * Returns null if the iterable is empty.
154 * Otherwise returns an element [:x:] of this [Iterable] so that
155 * [:x:] is not greater than [:y:] (that is, [:compare(x, y) <= 0:]) for all
156 * other elements [:y:] in the iterable.
157 *
158 * The [compare] function must be a proper [Comparator<T>]. If a function is
159 * not provided, [compare] defaults to [Comparable.compare].
160 */
161 E min([int compare(E a, E b)]) {
162 if (compare == null) compare = Comparable.compare;
163 Iterator it = iterator;
164 if (!it.moveNext()) return null;
165 E min = it.current;
166 while (it.moveNext) {
Sean Eagan 2013/01/02 16:35:13 it.moveNext()
167 E current = it.current;
168 if (compare(min, current) > 0) min = current;
169 }
170 return min;
171 }
172
173 /**
174 * Find the largest element in the iterable.
175 *
176 * Returns null if the iterable is empty.
177 * Otherwise returns an element [:x:] of this [Iterable] so that
178 * [:x:] is not smaller than [:y:] (that is, [:compare(x, y) >= 0:]) for all
179 * other elements [:y:] in the iterable.
180 *
181 * The [compare] function must be a proper [Comparator<T>]. If a function is
182 * not provided, [compare] defaults to [Comparable.compare].
183 */
184 E max([int compare(E a, E b)]) {
185 if (compare == null) compare = Comparable.compare;
186 Iterator it = iterator;
187 if (!it.moveNext()) return null;
188 E max = it.current;
189 while (it.moveNext) {
Sean Eagan 2013/01/02 16:35:13 it.moveNext()
190 E current = it.current;
191 if (compare(max, current) < 0) max = current;
192 }
193 return max;
194 }
195
196 /**
151 * Returns true if there is no element in this collection. 197 * Returns true if there is no element in this collection.
152 */ 198 */
153 bool get isEmpty => !iterator.moveNext(); 199 bool get isEmpty => !iterator.moveNext();
154 200
155 /** 201 /**
156 * Returns an [Iterable] with at most [n] elements. 202 * Returns an [Iterable] with at most [n] elements.
157 * 203 *
158 * The returned [Iterable] may contain fewer than [n] elements, if [this] 204 * The returned [Iterable] may contain fewer than [n] elements, if [this]
159 * contains fewer than [n] elements. 205 * contains fewer than [n] elements.
160 */ 206 */
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 _hasSkipped = true; 565 _hasSkipped = true;
520 while (_iterator.moveNext()) { 566 while (_iterator.moveNext()) {
521 if (!_f(_iterator.current)) return true; 567 if (!_f(_iterator.current)) return true;
522 } 568 }
523 } 569 }
524 return _iterator.moveNext(); 570 return _iterator.moveNext();
525 } 571 }
526 572
527 E get current => _iterator.current; 573 E get current => _iterator.current;
528 } 574 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698