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