Chromium Code Reviews| 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 /** | 5 /** |
| 6 * The [Iterable] interface allows to get an [Iterator] out of an | 6 * The [Iterable] interface groups functionality that can be expressed on top |
| 7 * [Iterable] object. | 7 * of [Iterator]s. |
| 8 * | 8 * |
| 9 * This interface is used by the for-in construct to iterate over an | 9 * [Iterable]s provide an [iterator] getter and can thus be used as the |
| 10 * [Iterable] object. | 10 * right-hand side of a for-in construct. |
| 11 * The for-in construct takes an [Iterable] object at the right-hand | |
| 12 * side, and calls its [iterator] method to get an [Iterator] on it. | |
| 13 * | |
| 14 * A user-defined class that implements the [Iterable] interface can | |
| 15 * be used as the right-hand side of a for-in construct. | |
| 16 */ | 11 */ |
| 17 abstract class Iterable<E> { | 12 abstract class Iterable<E> { |
| 18 /** | 13 /** |
| 19 * Returns an [Iterator] that iterates over this [Iterable] object. | 14 * Returns an [Iterator] that iterates over this [Iterable] object. |
| 20 */ | 15 */ |
| 21 Iterator<E> iterator(); | 16 Iterator<E> get iterator; |
| 17 | |
| 18 /** | |
| 19 * Returns a [Queryable] view of [this]. | |
| 20 */ | |
| 21 Queryable<E> query(); | |
|
Lasse Reichstein Nielsen
2012/11/12 09:38:03
Make it a getter. Even if it returns a new Queryab
floitsch
2012/11/16 22:31:07
the name is a verb. -> no getter. But if we change
Lasse Reichstein Nielsen
2012/11/19 13:36:09
But it IS a noun ?!? :)
| |
| 22 | |
| 23 /** | |
| 24 * Returns a lazy [Queryable] where each element [:e:] of [this] is replaced | |
| 25 * by the result of [:f(e):]. | |
| 26 * | |
| 27 * This method returns a view of the mapped elements. As long as the | |
| 28 * returned [Queryable] is not iterated over, the supplied function [f] will | |
| 29 * not be invoked. The transformed elements will not be cached. Iterating | |
| 30 * multiple times over the the returned [Queryable] will invoke the supplied | |
| 31 * function [f] multiple times on the same element. | |
| 32 */ | |
| 33 Queryable mappedBy(f(E element)); | |
| 34 | |
| 35 /** | |
| 36 * Returns a lazy [Queryable] with all elements [:e:] that satisfy the | |
|
Lasse Reichstein Nielsen
2012/11/12 09:38:03
You don't need to name 'e' if you don't use it lat
floitsch
2012/11/16 22:31:07
Done.
| |
| 37 * predicate [f]. | |
| 38 * | |
| 39 * This method returns a view of the mapped elements. As long as the | |
| 40 * returned [Queryable] is not iterated over, the supplied function [f] will | |
| 41 * not be invoked. Iterating will not cache results, and thus iterating | |
| 42 * multiple times over the the returned [Queryable] will invoke the supplied | |
| 43 * function [f] multiple times on the same element. | |
| 44 */ | |
| 45 Queryable<E> where(bool f(E element)); | |
| 46 | |
| 47 /** | |
| 48 * Checks whether [this] contains an element equal to [element]. This | |
| 49 * operation can be slow if [this] needs to iterate over all elements. | |
| 50 */ | |
| 51 bool contains(E element) { | |
| 52 for (E e in this) { | |
| 53 if (e == element) return true; | |
| 54 } | |
| 55 return false; | |
| 56 } | |
| 57 | |
| 58 /** | |
| 59 * Applies the function [f] to each element of [this] by iterating over it. | |
| 60 */ | |
| 61 void forEach(void f(E element)) { | |
| 62 for (E element in this) f(element); | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * Reduces this [Iterable] to a single value by iterating over [this] and | |
| 67 * iteratively combining each element of [this] with an existing | |
| 68 * value using the provided function. | |
| 69 * | |
| 70 * Uses [initialValue] as the initial value, and the function [combine] to | |
| 71 * create a new value from the previous one and an element. | |
| 72 * | |
| 73 * Example of calculating the sum of an [Iterable]: | |
| 74 * | |
| 75 * collection.reduce(0, (prev, element) => prev + element); | |
| 76 */ | |
| 77 dynamic reduce(var initialValue, | |
| 78 dynamic combine(var previousValue, E element)) { | |
| 79 var value = initialValue; | |
| 80 for (E element in this) value = combine(value, element); | |
| 81 return value; | |
| 82 } | |
|
Lasse Reichstein Nielsen
2012/11/12 09:38:03
You often want a reduction that uses the first val
floitsch
2012/11/16 22:31:07
if we find a good way, why not.
| |
| 83 | |
| 84 /** | |
| 85 * Returns true if every element of [this] satisifies the predicate [f]. | |
| 86 * Returns false otherwise. | |
| 87 * | |
| 88 * The iteration of [this] is stopped as soon as one non-satisfying element is | |
| 89 * found. | |
| 90 */ | |
| 91 bool every(bool f(E element)) { | |
| 92 for (E element in this) { | |
| 93 if (!f(element)) return false; | |
| 94 } | |
| 95 return true; | |
| 96 } | |
| 97 | |
| 98 /** | |
| 99 * Returns true if at least one element of [this] satisfies the predicate [f]. | |
| 100 * Returns false otherwise. | |
| 101 * | |
| 102 * The iteration of [this] is stopped as soon as one satisfying element is | |
| 103 * found. | |
| 104 */ | |
| 105 bool any(bool f(E element)) { | |
| 106 for (E element in this) { | |
| 107 if (f(element)) return true; | |
| 108 } | |
| 109 return false; | |
| 110 } | |
| 111 | |
| 112 /** | |
| 113 * Returns true if there is no element in [this]. | |
| 114 */ | |
| 115 bool get isEmpty => !iterator().hasNext; | |
|
Lasse Reichstein Nielsen
2012/11/12 09:38:03
Consider adding 'hasElements' that has the opposit
floitsch
2012/11/16 22:31:07
hasElements is imho a bad name. I would prefer isN
| |
| 22 } | 116 } |
| OLD | NEW |