OLD | NEW |
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 * A collection of values, or "elements", that can be accessed sequentially. | 8 * A collection of values, or "elements", that can be accessed sequentially. |
9 * | 9 * |
10 * The elements of the iterable are accessed by getting an [Iterator] | 10 * The elements of the iterable are accessed by getting an [Iterator] |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 * This method returns a view of the mapped elements. As long as the | 147 * This method returns a view of the mapped elements. As long as the |
148 * returned [Iterable] is not iterated over, the supplied function [f] will | 148 * returned [Iterable] is not iterated over, the supplied function [f] will |
149 * not be invoked. The transformed elements will not be cached. Iterating | 149 * not be invoked. The transformed elements will not be cached. Iterating |
150 * multiple times over the returned [Iterable] will invoke the supplied | 150 * multiple times over the returned [Iterable] will invoke the supplied |
151 * function [f] multiple times on the same element. | 151 * function [f] multiple times on the same element. |
152 * | 152 * |
153 * Methods on the returned iterable are allowed to omit calling `f` | 153 * Methods on the returned iterable are allowed to omit calling `f` |
154 * on any element where the result isn't needed. | 154 * on any element where the result isn't needed. |
155 * For example, [elementAt] may call `f` only once. | 155 * For example, [elementAt] may call `f` only once. |
156 */ | 156 */ |
157 Iterable/*<T>*/ map/*<T>*/(/*=T*/ f(E e)) => | 157 Iterable<T> map<T>(T f(E e)) => new MappedIterable<E, T>(this, f); |
158 new MappedIterable<E, dynamic/*=T*/>(this, f); | |
159 | 158 |
160 /** | 159 /** |
161 * Returns a new lazy [Iterable] with all elements that satisfy the | 160 * Returns a new lazy [Iterable] with all elements that satisfy the |
162 * predicate [test]. | 161 * predicate [test]. |
163 * | 162 * |
164 * The matching elements have the same order in the returned iterable | 163 * The matching elements have the same order in the returned iterable |
165 * as they have in [iterator]. | 164 * as they have in [iterator]. |
166 * | 165 * |
167 * This method returns a view of the mapped elements. | 166 * This method returns a view of the mapped elements. |
168 * As long as the returned [Iterable] is not iterated over, | 167 * As long as the returned [Iterable] is not iterated over, |
(...skipping 17 matching lines...) Expand all Loading... |
186 * | 185 * |
187 * var pairs = [[1, 2], [3, 4]]; | 186 * var pairs = [[1, 2], [3, 4]]; |
188 * var flattened = pairs.expand((pair) => pair).toList(); | 187 * var flattened = pairs.expand((pair) => pair).toList(); |
189 * print(flattened); // => [1, 2, 3, 4]; | 188 * print(flattened); // => [1, 2, 3, 4]; |
190 * | 189 * |
191 * var input = [1, 2, 3]; | 190 * var input = [1, 2, 3]; |
192 * var duplicated = input.expand((i) => [i, i]).toList(); | 191 * var duplicated = input.expand((i) => [i, i]).toList(); |
193 * print(duplicated); // => [1, 1, 2, 2, 3, 3] | 192 * print(duplicated); // => [1, 1, 2, 2, 3, 3] |
194 * | 193 * |
195 */ | 194 */ |
196 Iterable/*<T>*/ expand/*<T>*/(Iterable/*<T>*/ f(E element)) => | 195 Iterable<T> expand<T>(Iterable<T> f(E element)) => |
197 new ExpandIterable<E, dynamic/*=T*/>(this, f); | 196 new ExpandIterable<E, T>(this, f); |
198 | 197 |
199 /** | 198 /** |
200 * Returns true if the collection contains an element equal to [element]. | 199 * Returns true if the collection contains an element equal to [element]. |
201 * | 200 * |
202 * This operation will check each element in order for being equal to | 201 * This operation will check each element in order for being equal to |
203 * [element], unless it has a more efficient way to find an element | 202 * [element], unless it has a more efficient way to find an element |
204 * equal to [element]. | 203 * equal to [element]. |
205 * | 204 * |
206 * The equality used to determine whether [element] is equal to an element of | 205 * The equality used to determine whether [element] is equal to an element of |
207 * the iterable defaults to the [Object.==] of the element. | 206 * the iterable defaults to the [Object.==] of the element. |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 * for (E element in this) { | 272 * for (E element in this) { |
274 * value = combine(value, element); | 273 * value = combine(value, element); |
275 * } | 274 * } |
276 * return value; | 275 * return value; |
277 * | 276 * |
278 * Example of calculating the sum of an iterable: | 277 * Example of calculating the sum of an iterable: |
279 * | 278 * |
280 * iterable.fold(0, (prev, element) => prev + element); | 279 * iterable.fold(0, (prev, element) => prev + element); |
281 * | 280 * |
282 */ | 281 */ |
283 dynamic/*=T*/ fold/*<T>*/(var/*=T*/ initialValue, | 282 T fold<T>(T initialValue, T combine(T previousValue, E element)) { |
284 dynamic/*=T*/ combine(var/*=T*/ previousValue, E element)) { | |
285 var value = initialValue; | 283 var value = initialValue; |
286 for (E element in this) value = combine(value, element); | 284 for (E element in this) value = combine(value, element); |
287 return value; | 285 return value; |
288 } | 286 } |
289 | 287 |
290 /** | 288 /** |
291 * Checks whether every element of this iterable satisfies [test]. | 289 * Checks whether every element of this iterable satisfies [test]. |
292 * | 290 * |
293 * Checks every element in iteration order, and returns `false` if | 291 * Checks every element in iteration order, and returns `false` if |
294 * any of them make [test] return `false`, otherwise returns `true`. | 292 * any of them make [test] return `false`, otherwise returns `true`. |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 */ | 645 */ |
648 abstract class BidirectionalIterator<E> implements Iterator<E> { | 646 abstract class BidirectionalIterator<E> implements Iterator<E> { |
649 /** | 647 /** |
650 * Move back to the previous element. | 648 * Move back to the previous element. |
651 * | 649 * |
652 * Returns true and updates [current] if successful. Returns false | 650 * Returns true and updates [current] if successful. Returns false |
653 * and sets [current] to null if there is no previous element. | 651 * and sets [current] to null if there is no previous element. |
654 */ | 652 */ |
655 bool movePrevious(); | 653 bool movePrevious(); |
656 } | 654 } |
OLD | NEW |