| Index: sdk/lib/core/iterable.dart
|
| diff --git a/sdk/lib/core/iterable.dart b/sdk/lib/core/iterable.dart
|
| index 19972c12fd94c46ef1e36b2a13e5140d46624d91..421fd36de716ee2f2be5925ae2b4de8b31b344bd 100644
|
| --- a/sdk/lib/core/iterable.dart
|
| +++ b/sdk/lib/core/iterable.dart
|
| @@ -3,20 +3,116 @@
|
| // BSD-style license that can be found in the LICENSE file.
|
|
|
| /**
|
| - * The [Iterable] interface allows to get an [Iterator] out of an
|
| - * [Iterable] object.
|
| + * The [Iterable] interface groups functionality that can be expressed on top
|
| + * of [Iterator]s.
|
| *
|
| - * This interface is used by the for-in construct to iterate over an
|
| - * [Iterable] object.
|
| - * The for-in construct takes an [Iterable] object at the right-hand
|
| - * side, and calls its [iterator] method to get an [Iterator] on it.
|
| - *
|
| - * A user-defined class that implements the [Iterable] interface can
|
| - * be used as the right-hand side of a for-in construct.
|
| + * [Iterable]s provide an [iterator] getter and can thus be used as the
|
| + * right-hand side of a for-in construct.
|
| */
|
| abstract class Iterable<E> {
|
| + const Iterable();
|
| +
|
| /**
|
| * Returns an [Iterator] that iterates over this [Iterable] object.
|
| */
|
| - Iterator<E> iterator();
|
| + Iterator<E> get iterator;
|
| +
|
| + /**
|
| + * Returns a [Queryable] view of [this].
|
| + */
|
| + Queryable<E> query();
|
| +
|
| + /**
|
| + * Returns a lazy [Queryable] where each element [:e:] of [this] is replaced
|
| + * by the result of [:f(e):].
|
| + *
|
| + * This method returns a view of the mapped elements. As long as the
|
| + * returned [Queryable] is not iterated over, the supplied function [f] will
|
| + * not be invoked. The transformed elements will not be cached. Iterating
|
| + * multiple times over the the returned [Queryable] will invoke the supplied
|
| + * function [f] multiple times on the same element.
|
| + */
|
| + Queryable mappedBy(f(E element));
|
| +
|
| + /**
|
| + * Returns a lazy [Queryable] with all elements that satisfy the
|
| + * predicate [f].
|
| + *
|
| + * This method returns a view of the mapped elements. As long as the
|
| + * returned [Queryable] is not iterated over, the supplied function [f] will
|
| + * not be invoked. Iterating will not cache results, and thus iterating
|
| + * multiple times over the the returned [Queryable] will invoke the supplied
|
| + * function [f] multiple times on the same element.
|
| + */
|
| + Queryable<E> where(bool f(E element));
|
| +
|
| + /**
|
| + * Checks whether [this] contains an element equal to [element]. This
|
| + * operation can be slow if [this] needs to iterate over all elements.
|
| + */
|
| + 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] by iterating over it.
|
| + */
|
| + void forEach(void f(E element)) {
|
| + for (E element in this) f(element);
|
| + }
|
| +
|
| + /**
|
| + * Reduces this [Iterable] to a single value by iterating over [this] and
|
| + * iteratively combining each element of [this] with an existing
|
| + * value using the provided function.
|
| + *
|
| + * Uses [initialValue] as the initial value, and the function [combine] to
|
| + * create a new value from the previous one and an element.
|
| + *
|
| + * Example of calculating the sum of an [Iterable]:
|
| + *
|
| + * collection.reduce(0, (prev, element) => prev + element);
|
| + */
|
| + dynamic reduce(var initialValue,
|
| + dynamic combine(var previousValue, E element)) {
|
| + var value = initialValue;
|
| + for (E element in this) value = combine(value, element);
|
| + return value;
|
| + }
|
| +
|
| + /**
|
| + * Returns true if every element of [this] satisifies the predicate [f].
|
| + * Returns false otherwise.
|
| + *
|
| + * The iteration of [this] is stopped as soon as one non-satisfying element is
|
| + * found.
|
| + */
|
| + bool every(bool f(E element)) {
|
| + for (E element in this) {
|
| + if (!f(element)) return false;
|
| + }
|
| + return true;
|
| + }
|
| +
|
| + /**
|
| + * Returns true if at least one element of [this] satisfies the predicate [f].
|
| + * Returns false otherwise.
|
| + *
|
| + * The iteration of [this] is stopped as soon as one satisfying element is
|
| + * found.
|
| + */
|
| + bool any(bool f(E element)) {
|
| + for (E element in this) {
|
| + if (f(element)) return true;
|
| + }
|
| + return false;
|
| + }
|
| +
|
| + /**
|
| + * Returns true if [this] is empty.
|
| + */
|
| + bool get isEmpty => !iterator.moveNext();
|
| }
|
|
|