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 allows to get an [Iterator] out of an |
| 7 * [Iterable] object. | 7 * [Iterable] object. |
| 8 * | 8 * |
| 9 * This interface is used by the for-in construct to iterate over an | 9 * This interface is used by the for-in construct to iterate over an |
| 10 * [Iterable] object. | 10 * [Iterable] object. |
| 11 * The for-in construct takes an [Iterable] object at the right-hand | 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. | 12 * side, and calls its [iterator] method to get an [Iterator] on it. |
| 13 * | 13 * |
| 14 * A user-defined class that implements the [Iterable] interface can | 14 * A user-defined class that implements the [Iterable] interface can |
| 15 * be used as the right-hand side of a for-in construct. | 15 * be used as the right-hand side of a for-in construct. |
| 16 */ | 16 */ |
| 17 abstract class Iterable<E> { | 17 abstract class Iterable<E> { |
| 18 const Iterable(); | |
| 19 | |
| 18 /** | 20 /** |
| 19 * Returns an [Iterator] that iterates over this [Iterable] object. | 21 * Returns an [Iterator] that iterates over this [Iterable] object. |
| 20 */ | 22 */ |
| 21 Iterator<E> iterator(); | 23 Iterator<E> iterator(); |
| 22 | 24 |
| 23 /** | 25 /** |
| 24 * Returns a new collection with the elements [: f(e) :] | 26 * Returns a new collection with the elements [: f(e) :] |
| 25 * for each element [:e:] of this collection. | 27 * for each element [:e:] of this collection. |
| 26 * | 28 * |
| 27 * Subclasses of [Collection] should implement the [mappedBy] method | 29 * Subclasses of [Collection] should implement the [mappedBy] method |
| 28 * to return a collection of the same general type as themselves. | 30 * to return a collection of the same general type as themselves. |
| 29 * E.g., [List.mappedBy] should return a [List]. | 31 * E.g., [List.mappedBy] should return a [List]. |
| 30 */ | 32 */ |
| 31 Collection mappedBy(f(E element)); | 33 Collection mappedBy(f(E element)) { |
| 34 // TODO(floitsch): this is just a temporary function to provide a complete | |
| 35 // skeleton. It will be changed to become lazy soon. | |
|
sra1
2012/11/15 01:02:18
How will it be lazy?
floitsch
2012/11/15 01:27:27
Have a look here:
https://chromiumcodereview.appsp
| |
| 36 List result = new List(); | |
|
sra1
2012/11/15 01:02:18
Any reason to use new List() when the more concise
floitsch
2012/11/15 01:27:27
Done.
| |
| 37 for (E element in this) result.add(f(element)); | |
| 38 return result; | |
| 39 } | |
| 32 | 40 |
| 33 /** | 41 /** |
| 34 * Returns a collection with the elements of this collection | 42 * Returns a collection with the elements of this collection |
| 35 * that satisfy the predicate [f]. | 43 * that satisfy the predicate [f]. |
| 36 * | 44 * |
| 37 * The returned collection should be of the same type as the collection | 45 * The returned collection should be of the same type as the collection |
| 38 * creating it. | 46 * creating it. |
| 39 * | 47 * |
| 40 * An element satisfies the predicate [f] if [:f(element):] | 48 * An element satisfies the predicate [f] if [:f(element):] |
| 41 * returns true. | 49 * returns true. |
| 42 */ | 50 */ |
| 43 Collection<E> where(bool f(E element)); | 51 Collection<E> where(bool f(E element)) { |
| 52 // TODO(floitsch): this is just a temporary function to provide a complete | |
| 53 // skeleton. It will be changed to become lazy soon. | |
| 54 List result = new List<E>(); | |
|
sra1
2012/11/15 01:02:18
Ditto <E>[]
floitsch
2012/11/15 01:27:27
Done.
| |
| 55 for (E element in this) if (f(element)) result.add(element); | |
| 56 return result; | |
| 57 } | |
| 44 | 58 |
| 45 /** | 59 /** |
| 46 * Check whether the collection contains an element equal to [element]. | 60 * Check whether the collection contains an element equal to [element]. |
| 47 */ | 61 */ |
| 48 bool contains(E element) { | 62 bool contains(E element) { |
| 49 for (E e in this) { | 63 for (E e in this) { |
| 50 if (e == element) return true; | 64 if (e == element) return true; |
| 51 } | 65 } |
| 52 return false; | 66 return false; |
| 53 } | 67 } |
| 54 | 68 |
| 55 /** | 69 /** |
| 56 * Applies the function [f] to each element of this collection. | 70 * Applies the function [f] to each element of this collection. |
| 57 */ | 71 */ |
| 58 void forEach(void f(E element)) { | 72 void forEach(void f(E element)) { |
| 59 for (E element in this) f(element); | 73 for (E element in this) f(element); |
| 60 } | 74 } |
| 61 | 75 |
| 62 /** | 76 /** |
| 63 * Reduce a collection to a single value by iteratively combining each element | 77 * Reduce a collection to a single value by iteratively combining each element |
| 64 * of the collection with an existing value using the provided function. | 78 * of the collection with an existing value using the provided function. |
| 65 * Use [initialValue] as the initial value, and the function [combine] to | 79 * Use [initialValue] as the initial value, and the function [combine] to |
| 66 * create a new value from the previous one and an element. | 80 * create a new value from the previous one and an element. |
| 67 * | 81 * |
| 68 * Example of calculating the sum of a collection: | 82 * Example of calculating the sum of a collection: |
| 69 * | 83 * |
| 70 * collection.reduce(0, (prev, element) => prev + element); | 84 * collection.reduce(0, (prev, element) => prev + element); |
| 71 */ | 85 */ |
| 72 Dynamic reduce(var initialValue, | 86 Dynamic reduce(var initialValue, |
|
sra1
2012/11/15 01:02:18
Dynamic -> dynamic ?
floitsch
2012/11/15 01:27:27
Done.
| |
| 73 Dynamic combine(var previousValue, E element)) { | 87 Dynamic combine(var previousValue, E element)) { |
| 74 var value = initialValue; | 88 var value = initialValue; |
| 75 for (E element in this) value = combine(value, element); | 89 for (E element in this) value = combine(value, element); |
| 76 return value; | 90 return value; |
| 77 } | 91 } |
| 78 | 92 |
| 79 /** | 93 /** |
| 80 * Returns true if every elements of this collection satisify the | 94 * Returns true if every elements of this collection satisify the |
| 81 * predicate [f]. Returns false otherwise. | 95 * predicate [f]. Returns false otherwise. |
| 82 */ | 96 */ |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 96 if (f(element)) return true; | 110 if (f(element)) return true; |
| 97 } | 111 } |
| 98 return false; | 112 return false; |
| 99 } | 113 } |
| 100 | 114 |
| 101 /** | 115 /** |
| 102 * Returns true if there is no element in this collection. | 116 * Returns true if there is no element in this collection. |
| 103 */ | 117 */ |
| 104 bool get isEmpty => !iterator().hasNext; | 118 bool get isEmpty => !iterator().hasNext; |
| 105 } | 119 } |
| OLD | NEW |