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

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: Rebase 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 06c8efa8b5f657b35e99ea0d8cc5fe306e986594..1213dcf68f05515287c6c24e7241fd4b70b8b0be 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(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 [Queryable] with all elements that satisfy the
Anders Johnsen 2012/11/20 06:32:21 Queryable?
floitsch 2012/11/28 13:49:38 Done.
+ * 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.
+ */
+ Iterable<E> where(bool f(E element)) => new FilteredIterable<E>(this, f);
/**
* Check whether the collection contains an element equal to [element].
@@ -139,6 +133,10 @@ class MappedIterable<E> extends Iterable<E> {
MappedIterable(this._iterable, E this._f(element));
Iterator<E> get iterator => new MappedIterator<E>(_iterable.iterator, _f);
+
+ // Length related functions are independent of the mapping.
+ int get length => _iterable.length;
+ bool get isEmpty => _iterable.isEmpty;
}
class MappedIterator<E> extends Iterator<E> {

Powered by Google App Engine
This is Rietveld 408576698