Chromium Code Reviews| Index: lib/core/collection.dart |
| diff --git a/lib/core/collection.dart b/lib/core/collection.dart |
| index ffad48d6c76913e76bb79efee8548fc3708b6b72..0c2a7427dd5f91055b8758f2380d44566849e601 100644 |
| --- a/lib/core/collection.dart |
| +++ b/lib/core/collection.dart |
| @@ -3,26 +3,57 @@ |
| // BSD-style license that can be found in the LICENSE file. |
| /** |
| - * The [Collection] interface is the public interface of all |
| - * collections. |
| + * The common interface of all collections. |
| + * |
| + * The [Collection] class contains a skeleton implementation of |
| + * an iterator based collection. |
| */ |
| abstract class Collection<E> extends Iterable<E> { |
| /** |
| - * Applies the function [f] to each element of this collection. |
| - */ |
| - void forEach(void f(E element)); |
| - |
| - /** |
| * Returns a new collection with the elements [: f(e) :] |
| * for each element [:e:] of this collection. |
| * |
| - * Note on typing: the return type of f() could be an arbitrary |
| - * type and consequently the returned collection's |
| - * typeis Collection. |
| + * Subclasses of [Collection] should implement the [map] method |
| + * to return a collection of the same general type as themselves. |
| + * E.g., [List.map] should return a [List]. |
| */ |
| Collection map(f(E element)); |
| /** |
| + * Returns a collection with the elements of this collection |
| + * that satisfy the predicate [f]. |
| + * |
| + * The returned collection should be of the same type as the collection |
| + * creating it. |
| + * |
| + * An element satisfies the predicate [f] if [:f(element):] |
| + * returns true. |
| + */ |
| + Collection<E> filter(bool f(E element)); |
| + |
| + /** |
| + * Returns the number of elements in this collection. |
| + */ |
| + int get length; |
| + |
| + /** |
| + * Check whether the collection contains an element equal to [element]. |
| + */ |
| + bool contains(E element) { |
| + for (E e in this) { |
| + if (e == element) return true; |
| + } |
| + return false; |
| + } |
| + |
| + /** |
| + * Applies the function [f] to each element of this collection. |
| + */ |
| + void forEach(void f(E element)) { |
| + for (E element in this) f(element); |
| + } |
| + |
| + /** |
| * Reduce a collection to a single value by iteratively combining each element |
| * of the collection with an existing value using the provided function. |
| * Use [initialValue] as the initial value, and the function [combine] to |
| @@ -32,37 +63,37 @@ abstract class Collection<E> extends Iterable<E> { |
| * |
| * collection.reduce(0, (prev, element) => prev + element); |
| */ |
| - Dynamic reduce(Dynamic initialValue, |
| - Dynamic combine(Dynamic previousValue, E element)); |
| - |
| - /** |
| - * Returns a new collection with the elements of this collection |
| - * that satisfy the predicate [f]. |
| - * |
| - * An element satisfies the predicate [f] if [:f(element):] |
| - * returns true. |
| - */ |
| - Collection<E> filter(bool f(E element)); |
| + Dynamic reduce(var initialValue, |
|
ngeoffray
2012/10/16 15:08:18
Why is this one 'var'?
sra1
2012/10/16 18:52:52
I thought the standard coding style was to leave o
Lasse Reichstein Nielsen
2012/10/17 11:06:45
I'm not sure it's *standard*, but it's definitely
|
| + Dynamic combine(var previousValue, E element)) { |
|
ngeoffray
2012/10/16 15:08:18
ditto
|
| + var value = initialValue; |
|
ngeoffray
2012/10/16 15:08:18
Why this?
sra1
2012/10/16 18:52:52
It would be wrong to overwrite 'initialValue' sinc
Lasse Reichstein Nielsen
2012/10/17 11:06:45
Exactly :)
|
| + for (Element element in this) value = combine(value, element); |
|
ngeoffray
2012/10/16 15:08:18
Element -> E
Lasse Reichstein Nielsen
2012/10/17 11:06:45
Thanks.
|
| + return value; |
| + } |
| /** |
| * Returns true if every elements of this collection satisify the |
| * predicate [f]. Returns false otherwise. |
| */ |
| - bool every(bool f(E element)); |
| + bool every(bool f(E element)) { |
| + for (E element in this) { |
| + if (!f(element)) return false; |
| + } |
| + return true; |
| + } |
| /** |
| * Returns true if one element of this collection satisfies the |
| * predicate [f]. Returns false otherwise. |
| */ |
| - bool some(bool f(E element)); |
| + bool some(bool f(E element)) { |
| + for (E element in this) { |
| + if (f(element)) return true; |
| + } |
| + return false; |
| + } |
| /** |
| * Returns true if there is no element in this collection. |
| */ |
| - bool isEmpty(); |
| - |
| - /** |
| - * Returns the number of elements in this collection. |
| - */ |
| - int get length; |
| + bool isEmpty() => !iterator().hasNext(); |
| } |