Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Unified Diff: sdk/lib/core/iterable.dart

Issue 11412086: Make 'where' lazy. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: FilteredIterable/Iterator -> WhereIterable/Iterator. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/core/iterable.dart
diff --git a/sdk/lib/core/iterable.dart b/sdk/lib/core/iterable.dart
index b753756802dfe34e2bc70290374b134097d5911d..d82e888014cdc7f9bec7f36dd7193018981edac0 100644
--- a/sdk/lib/core/iterable.dart
+++ b/sdk/lib/core/iterable.dart
@@ -35,22 +35,16 @@ abstract class Iterable<E> {
Iterable mappedBy(f(E element)) => new MappedIterable<E, dynamic>(this, f);
/**
- * 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> where(bool f(E element)) {
- // TODO(floitsch): this is just a temporary function to provide a complete
- // skeleton. It will be changed to become lazy soon.
- List result = <E>[];
- for (E element in this) if (f(element)) result.add(element);
- return result;
- }
+ * Returns a lazy [Iterable] with all elements that satisfy the
+ * predicate [f].
+ *
+ * This method returns a view of the mapped elements. As long as the
+ * returned [Iterable] 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 [Iterable] will invoke the supplied
+ * function [f] multiple times on the same element.
+ */
+ Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f);
/**
* Check whether the collection contains an element equal to [element].
@@ -139,6 +133,10 @@ class MappedIterable<S, T> extends Iterable<T> {
MappedIterable(this._iterable, T this._f(S element));
Iterator<T> get iterator => new MappedIterator<S, T>(_iterable.iterator, _f);
+
+ // Length related functions are independent of the mapping.
+ int get length => _iterable.length;
+ bool get isEmpty => _iterable.isEmpty;
}
class MappedIterator<S, T> extends Iterator<T> {
@@ -161,21 +159,21 @@ class MappedIterator<S, T> extends Iterator<T> {
T get current => _current;
}
-class FilteredIterable<E> extends Iterable<E> {
+class WhereIterable<E> extends Iterable<E> {
final Iterable<E> _iterable;
final Function _f;
- FilteredIterable(this._iterable, bool this._f(E element));
+ WhereIterable(this._iterable, bool this._f(E element));
- Iterator<E> get iterator => new FilteredIterator<E>(_iterable.iterator, _f);
+ Iterator<E> get iterator => new WhereIterator<E>(_iterable.iterator, _f);
}
-class FilteredIterator<E> extends Iterator<E> {
+class WhereIterator<E> extends Iterator<E> {
E _current;
Iterator _iterator;
Function _f;
- FilteredIterator(this._iterator, bool this._f(E element));
+ WhereIterator(this._iterator, bool this._f(E element));
bool moveNext() {
while (_iterator.moveNext()) {

Powered by Google App Engine
This is Rietveld 408576698