| 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 common interface of all collections. | 6 * The [Collection] interface is the public interface of all |
| 7 * | 7 * collections. |
| 8 * The [Collection] class contains a skeleton implementation of | |
| 9 * an iterator based collection. | |
| 10 */ | 8 */ |
| 11 abstract class Collection<E> extends Iterable<E> { | 9 abstract class Collection<E> extends Iterable<E> { |
| 12 /** | 10 /** |
| 11 * Applies the function [f] to each element of this collection. |
| 12 */ |
| 13 void forEach(void f(E element)); |
| 14 |
| 15 /** |
| 13 * Returns a new collection with the elements [: f(e) :] | 16 * Returns a new collection with the elements [: f(e) :] |
| 14 * for each element [:e:] of this collection. | 17 * for each element [:e:] of this collection. |
| 15 * | 18 * |
| 16 * Subclasses of [Collection] should implement the [map] method | 19 * Note on typing: the return type of f() could be an arbitrary |
| 17 * to return a collection of the same general type as themselves. | 20 * type and consequently the returned collection's |
| 18 * E.g., [List.map] should return a [List]. | 21 * typeis Collection. |
| 19 */ | 22 */ |
| 20 Collection map(f(E element)); | 23 Collection map(f(E element)); |
| 21 | 24 |
| 22 /** | 25 /** |
| 23 * Returns a collection with the elements of this collection | |
| 24 * that satisfy the predicate [f]. | |
| 25 * | |
| 26 * The returned collection should be of the same type as the collection | |
| 27 * creating it. | |
| 28 * | |
| 29 * An element satisfies the predicate [f] if [:f(element):] | |
| 30 * returns true. | |
| 31 */ | |
| 32 Collection<E> filter(bool f(E element)); | |
| 33 | |
| 34 /** | |
| 35 * Returns the number of elements in this collection. | |
| 36 */ | |
| 37 int get length; | |
| 38 | |
| 39 /** | |
| 40 * Check whether the collection contains an element equal to [element]. | |
| 41 */ | |
| 42 bool contains(E element) { | |
| 43 for (E e in this) { | |
| 44 if (e == element) return true; | |
| 45 } | |
| 46 return false; | |
| 47 } | |
| 48 | |
| 49 /** | |
| 50 * Applies the function [f] to each element of this collection. | |
| 51 */ | |
| 52 void forEach(void f(E element)) { | |
| 53 for (E element in this) f(element); | |
| 54 } | |
| 55 | |
| 56 /** | |
| 57 * Reduce a collection to a single value by iteratively combining each element | 26 * Reduce a collection to a single value by iteratively combining each element |
| 58 * of the collection with an existing value using the provided function. | 27 * of the collection with an existing value using the provided function. |
| 59 * Use [initialValue] as the initial value, and the function [combine] to | 28 * Use [initialValue] as the initial value, and the function [combine] to |
| 60 * create a new value from the previous one and an element. | 29 * create a new value from the previous one and an element. |
| 61 * | 30 * |
| 62 * Example of calculating the sum of a collection: | 31 * Example of calculating the sum of a collection: |
| 63 * | 32 * |
| 64 * collection.reduce(0, (prev, element) => prev + element); | 33 * collection.reduce(0, (prev, element) => prev + element); |
| 65 */ | 34 */ |
| 66 Dynamic reduce(var initialValue, | 35 Dynamic reduce(Dynamic initialValue, |
| 67 Dynamic combine(var previousValue, E element)) { | 36 Dynamic combine(Dynamic previousValue, E element)); |
| 68 var value = initialValue; | 37 |
| 69 for (E element in this) value = combine(value, element); | 38 /** |
| 70 return value; | 39 * Returns a new collection with the elements of this collection |
| 71 } | 40 * that satisfy the predicate [f]. |
| 41 * |
| 42 * An element satisfies the predicate [f] if [:f(element):] |
| 43 * returns true. |
| 44 */ |
| 45 Collection<E> filter(bool f(E element)); |
| 72 | 46 |
| 73 /** | 47 /** |
| 74 * Returns true if every elements of this collection satisify the | 48 * Returns true if every elements of this collection satisify the |
| 75 * predicate [f]. Returns false otherwise. | 49 * predicate [f]. Returns false otherwise. |
| 76 */ | 50 */ |
| 77 bool every(bool f(E element)) { | 51 bool every(bool f(E element)); |
| 78 for (E element in this) { | |
| 79 if (!f(element)) return false; | |
| 80 } | |
| 81 return true; | |
| 82 } | |
| 83 | 52 |
| 84 /** | 53 /** |
| 85 * Returns true if one element of this collection satisfies the | 54 * Returns true if one element of this collection satisfies the |
| 86 * predicate [f]. Returns false otherwise. | 55 * predicate [f]. Returns false otherwise. |
| 87 */ | 56 */ |
| 88 bool some(bool f(E element)) { | 57 bool some(bool f(E element)); |
| 89 for (E element in this) { | |
| 90 if (f(element)) return true; | |
| 91 } | |
| 92 return false; | |
| 93 } | |
| 94 | 58 |
| 95 /** | 59 /** |
| 96 * Returns true if there is no element in this collection. | 60 * Returns true if there is no element in this collection. |
| 97 */ | 61 */ |
| 98 bool isEmpty() => !iterator().hasNext(); | 62 bool isEmpty(); |
| 63 |
| 64 /** |
| 65 * Returns the number of elements in this collection. |
| 66 */ |
| 67 int get length; |
| 99 } | 68 } |
| OLD | NEW |