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

Side by Side Diff: sdk/lib/core/iterable.dart

Issue 14071002: Added new version of reduce. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Removed more uses of max, and a few bugs. Created 7 years, 8 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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 } 86 }
87 87
88 /** 88 /**
89 * Applies the function [f] to each element of this collection. 89 * Applies the function [f] to each element of this collection.
90 */ 90 */
91 void forEach(void f(E element)) { 91 void forEach(void f(E element)) {
92 for (E element in this) f(element); 92 for (E element in this) f(element);
93 } 93 }
94 94
95 /** 95 /**
96 * Reduce a collection to a single value by iteratively combining each element 96 * Reduce a collection to a single value by iteratively combining elements
97 * of the collection with an existing value using the provided function. 97 * of the collection using the provided function.
98 * Use [initialValue] as the initial value, and the function [combine] to
99 * create a new value from the previous one and an element.
100 * 98 *
101 * Example of calculating the sum of an iterable: 99 * Example of calculating the sum of an iterable:
102 * 100 *
103 * iterable.reduce((prev, element) => prev + element); 101 * iterable.reduce((value, element) => value + element);
104 * 102 *
105 * *UPCOMING API-CHANGE*: this method will soon be changed to not take
106 * an initial value: `iterable.reduce(min)`. Use [fold] instead.
107 */ 103 */
108 @deprecated 104 E reduce(E combine(E value, E element)) {
109 dynamic reduce(var initialValue, 105 Iterator<E> iterator = this.iterator;
110 dynamic combine(var previousValue, E element)) { 106 if (!iterator.moveNext()) {
111 return fold(initialValue, combine); 107 throw new StateError("No elements");
108 }
109 E value = iterator.current;
110 while (iterator.moveNext()) {
111 value = combine(value, iterator.current);
112 }
113 return value;
112 } 114 }
113 115
114 /** 116 /**
115 * Reduce a collection to a single value by iteratively combining each element 117 * Reduce a collection to a single value by iteratively combining each element
116 * of the collection with an existing value using the provided function. 118 * of the collection with an existing value using the provided function.
117 * Use [initialValue] as the initial value, and the function [combine] to 119 * Use [initialValue] as the initial value, and the function [combine] to
118 * create a new value from the previous one and an element. 120 * create a new value from the previous one and an element.
119 * 121 *
120 * Example of calculating the sum of an iterable: 122 * Example of calculating the sum of an iterable:
121 * 123 *
122 * iterable.fold(0, (prev, element) => prev + element); 124 * iterable.fold(0, (prev, element) => prev + element);
125 *
123 */ 126 */
124 dynamic fold(var initialValue, 127 dynamic fold(var initialValue,
125 dynamic combine(var previousValue, E element)) { 128 dynamic combine(var previousValue, E element)) {
126 var value = initialValue; 129 var value = initialValue;
127 for (E element in this) value = combine(value, element); 130 for (E element in this) value = combine(value, element);
128 return value; 131 return value;
129 } 132 }
130 133
131 /** 134 /**
132 * Returns true if every elements of this collection satisify the 135 * Returns true if every elements of this collection satisify the
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 int get length { 179 int get length {
177 int count = 0; 180 int count = 0;
178 Iterator it = iterator; 181 Iterator it = iterator;
179 while (it.moveNext()) { 182 while (it.moveNext()) {
180 count++; 183 count++;
181 } 184 }
182 return count; 185 return count;
183 } 186 }
184 187
185 /** 188 /**
186 * Find the least element in the iterable.
187 *
188 * Returns null if the iterable is empty.
189 * Otherwise returns an element [:x:] of this [Iterable] so that
190 * [:x:] is not greater than [:y:] (that is, [:compare(x, y) <= 0:]) for all
191 * other elements [:y:] in the iterable.
192 *
193 * The [compare] function must be a proper [Comparator<T>]. If a function is
194 * not provided, [compare] defaults to [Comparable.compare].
195 *
196 * *Deprecated*. Use [reduce] with a binary min method if needed.
197 */
198 @deprecated
199 E min([int compare(E a, E b)]) {
200 if (compare == null) compare = Comparable.compare;
201 Iterator it = iterator;
202 if (!it.moveNext()) return null;
203 E min = it.current;
204 while (it.moveNext()) {
205 E current = it.current;
206 if (compare(min, current) > 0) min = current;
207 }
208 return min;
209 }
210
211 /**
212 * Find the largest element in the iterable.
213 *
214 * Returns null if the iterable is empty.
215 * Otherwise returns an element [:x:] of this [Iterable] so that
216 * [:x:] is not smaller than [:y:] (that is, [:compare(x, y) >= 0:]) for all
217 * other elements [:y:] in the iterable.
218 *
219 * The [compare] function must be a proper [Comparator<T>]. If a function is
220 * not provided, [compare] defaults to [Comparable.compare].
221 *
222 * *Deprecated*. Use [reduce] with a binary max method if needed.
223 */
224 @deprecated
225 E max([int compare(E a, E b)]) {
226 if (compare == null) compare = Comparable.compare;
227 Iterator it = iterator;
228 if (!it.moveNext()) return null;
229 E max = it.current;
230 while (it.moveNext()) {
231 E current = it.current;
232 if (compare(max, current) < 0) max = current;
233 }
234 return max;
235 }
236
237 /**
238 * Returns true if there is no element in this collection. 189 * Returns true if there is no element in this collection.
239 */ 190 */
240 bool get isEmpty => !iterator.moveNext(); 191 bool get isEmpty => !iterator.moveNext();
241 192
242 /** 193 /**
243 * Returns an [Iterable] with at most [n] elements. 194 * Returns an [Iterable] with at most [n] elements.
244 * 195 *
245 * The returned [Iterable] may contain fewer than [n] elements, if [this] 196 * The returned [Iterable] may contain fewer than [n] elements, if [this]
246 * contains fewer than [n] elements. 197 * contains fewer than [n] elements.
247 */ 198 */
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 */ 397 */
447 abstract class BidirectionalIterator<T> extends Iterator<T> { 398 abstract class BidirectionalIterator<T> extends Iterator<T> {
448 /** 399 /**
449 * Move back to the previous element. 400 * Move back to the previous element.
450 * 401 *
451 * Returns true and updates [current] if successful. Returns false 402 * Returns true and updates [current] if successful. Returns false
452 * and sets [current] to null if there is no previous element. 403 * and sets [current] to null if there is no previous element.
453 */ 404 */
454 bool movePrevious(); 405 bool movePrevious();
455 } 406 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698