OLD | NEW |
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 * This [Iterable] mixin implements all [Iterable] members except `iterator`. | 8 * This [Iterable] mixin implements all [Iterable] members except `iterator`. |
9 * | 9 * |
10 * All other methods are implemented in terms of `iterator`. | 10 * All other methods are implemented in terms of `iterator`. |
11 */ | 11 */ |
12 abstract class IterableMixin<E> implements Iterable<E> { | 12 abstract class IterableMixin<E> implements Iterable<E> { |
13 // This class has methods copied verbatim into: | 13 // This class has methods copied verbatim into: |
14 // - IterableBase | 14 // - IterableBase |
15 // - SetMixin | 15 // - SetMixin |
16 // If changing a method here, also change the other copies. | 16 // If changing a method here, also change the other copies. |
17 | 17 |
18 Iterable map(f(E element)) => new MappedIterable<E, dynamic>(this, f); | 18 Iterable/*<T>*/ map/*<T>*/(/*=T*/ f(E element)) => |
| 19 new MappedIterable<E, dynamic/*=T*/>(this, f); |
19 | 20 |
20 Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f); | 21 Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f); |
21 | 22 |
22 Iterable expand(Iterable f(E element)) => | 23 Iterable/*<T>*/ expand/*<T>*/(Iterable/*<T>*/ f(E element)) => |
23 new ExpandIterable<E, dynamic>(this, f); | 24 new ExpandIterable<E, dynamic/*=T*/>(this, f); |
24 | 25 |
25 bool contains(Object element) { | 26 bool contains(Object element) { |
26 for (E e in this) { | 27 for (E e in this) { |
27 if (e == element) return true; | 28 if (e == element) return true; |
28 } | 29 } |
29 return false; | 30 return false; |
30 } | 31 } |
31 | 32 |
32 void forEach(void f(E element)) { | 33 void forEach(void f(E element)) { |
33 for (E element in this) f(element); | 34 for (E element in this) f(element); |
34 } | 35 } |
35 | 36 |
36 E reduce(E combine(E value, E element)) { | 37 E reduce(E combine(E value, E element)) { |
37 Iterator<E> iterator = this.iterator; | 38 Iterator<E> iterator = this.iterator; |
38 if (!iterator.moveNext()) { | 39 if (!iterator.moveNext()) { |
39 throw IterableElementError.noElement(); | 40 throw IterableElementError.noElement(); |
40 } | 41 } |
41 E value = iterator.current; | 42 E value = iterator.current; |
42 while (iterator.moveNext()) { | 43 while (iterator.moveNext()) { |
43 value = combine(value, iterator.current); | 44 value = combine(value, iterator.current); |
44 } | 45 } |
45 return value; | 46 return value; |
46 } | 47 } |
47 | 48 |
48 dynamic fold(var initialValue, | 49 dynamic/*=T*/ fold/*<T>*/(var/*=T*/ initialValue, |
49 dynamic combine(var previousValue, E element)) { | 50 dynamic/*=T*/ combine(var/*=T*/ previousValue, E element)) { |
50 var value = initialValue; | 51 var value = initialValue; |
51 for (E element in this) value = combine(value, element); | 52 for (E element in this) value = combine(value, element); |
52 return value; | 53 return value; |
53 } | 54 } |
54 | 55 |
55 bool every(bool f(E element)) { | 56 bool every(bool f(E element)) { |
56 for (E element in this) { | 57 for (E element in this) { |
57 if (!f(element)) return false; | 58 if (!f(element)) return false; |
58 } | 59 } |
59 return true; | 60 return true; |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 * Base class for implementing [Iterable]. | 205 * Base class for implementing [Iterable]. |
205 * | 206 * |
206 * This class implements all methods of [Iterable] except [Iterable.iterator] | 207 * This class implements all methods of [Iterable] except [Iterable.iterator] |
207 * in terms of `iterator`. | 208 * in terms of `iterator`. |
208 */ | 209 */ |
209 abstract class IterableBase<E> implements Iterable<E> { | 210 abstract class IterableBase<E> implements Iterable<E> { |
210 // TODO(lrn): Base this on IterableMixin if there ever becomes a way | 211 // TODO(lrn): Base this on IterableMixin if there ever becomes a way |
211 // to combine const constructors and mixins. | 212 // to combine const constructors and mixins. |
212 const IterableBase(); | 213 const IterableBase(); |
213 | 214 |
214 Iterable map(f(E element)) => new MappedIterable<E, dynamic>(this, f); | 215 Iterable/*<T>*/ map/*<T>*/(/*=T*/f(E element)) => new MappedIterable<E, dynami
c/*=T*/>(this, f); |
215 | 216 |
216 Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f); | 217 Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f); |
217 | 218 |
218 Iterable expand(Iterable f(E element)) => | 219 Iterable/*<T>*/ expand/*<T>*/(Iterable/*<T>*/ f(E element)) => |
219 new ExpandIterable<E, dynamic>(this, f); | 220 new ExpandIterable<E, dynamic/*=T*/>(this, f); |
220 | 221 |
221 bool contains(Object element) { | 222 bool contains(Object element) { |
222 for (E e in this) { | 223 for (E e in this) { |
223 if (e == element) return true; | 224 if (e == element) return true; |
224 } | 225 } |
225 return false; | 226 return false; |
226 } | 227 } |
227 | 228 |
228 void forEach(void f(E element)) { | 229 void forEach(void f(E element)) { |
229 for (E element in this) f(element); | 230 for (E element in this) f(element); |
230 } | 231 } |
231 | 232 |
232 E reduce(E combine(E value, E element)) { | 233 E reduce(E combine(E value, E element)) { |
233 Iterator<E> iterator = this.iterator; | 234 Iterator<E> iterator = this.iterator; |
234 if (!iterator.moveNext()) { | 235 if (!iterator.moveNext()) { |
235 throw IterableElementError.noElement(); | 236 throw IterableElementError.noElement(); |
236 } | 237 } |
237 E value = iterator.current; | 238 E value = iterator.current; |
238 while (iterator.moveNext()) { | 239 while (iterator.moveNext()) { |
239 value = combine(value, iterator.current); | 240 value = combine(value, iterator.current); |
240 } | 241 } |
241 return value; | 242 return value; |
242 } | 243 } |
243 | 244 |
244 dynamic fold(var initialValue, | 245 dynamic/*=T*/ fold/*<T>*/(var/*=T*/ initialValue, |
245 dynamic combine(var previousValue, E element)) { | 246 dynamic/*=T*/ combine(var/*=T*/ previousValue, E ele
ment)) { |
246 var value = initialValue; | 247 var value = initialValue; |
247 for (E element in this) value = combine(value, element); | 248 for (E element in this) value = combine(value, element); |
248 return value; | 249 return value; |
249 } | 250 } |
250 | 251 |
251 bool every(bool f(E element)) { | 252 bool every(bool f(E element)) { |
252 for (E element in this) { | 253 for (E element in this) { |
253 if (!f(element)) return false; | 254 if (!f(element)) return false; |
254 } | 255 } |
255 return true; | 256 return true; |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
591 length += ELLIPSIS_SIZE + OVERHEAD; | 592 length += ELLIPSIS_SIZE + OVERHEAD; |
592 } | 593 } |
593 } | 594 } |
594 if (elision != null) { | 595 if (elision != null) { |
595 parts.add(elision); | 596 parts.add(elision); |
596 } | 597 } |
597 parts.add(penultimateString); | 598 parts.add(penultimateString); |
598 parts.add(ultimateString); | 599 parts.add(ultimateString); |
599 } | 600 } |
600 } | 601 } |
OLD | NEW |