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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 /** 5 /**
6 * The [Iterable] interface allows to get an [Iterator] out of an 6 * The [Iterable] interface allows to get an [Iterator] out of an
7 * [Iterable] object. 7 * [Iterable] object.
8 * 8 *
9 * This interface is used by the for-in construct to iterate over an 9 * This interface is used by the for-in construct to iterate over an
10 * [Iterable] object. 10 * [Iterable] object.
(...skipping 17 matching lines...) Expand all
28 * 28 *
29 * This method returns a view of the mapped elements. As long as the 29 * This method returns a view of the mapped elements. As long as the
30 * returned [Iterable] is not iterated over, the supplied function [f] will 30 * returned [Iterable] is not iterated over, the supplied function [f] will
31 * not be invoked. The transformed elements will not be cached. Iterating 31 * not be invoked. The transformed elements will not be cached. Iterating
32 * multiple times over the the returned [Iterable] will invoke the supplied 32 * multiple times over the the returned [Iterable] will invoke the supplied
33 * function [f] multiple times on the same element. 33 * function [f] multiple times on the same element.
34 */ 34 */
35 Iterable mappedBy(f(E element)) => new MappedIterable(this, f); 35 Iterable mappedBy(f(E element)) => new MappedIterable(this, f);
36 36
37 /** 37 /**
38 * Returns a collection with the elements of this collection 38 * 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.
39 * that satisfy the predicate [f]. 39 * predicate [f].
40 * 40 *
41 * The returned collection should be of the same type as the collection 41 * This method returns a view of the mapped elements. As long as the
42 * creating it. 42 * returned [Queryable] is not iterated over, the supplied function [f] will
43 * 43 * not be invoked. Iterating will not cache results, and thus iterating
44 * An element satisfies the predicate [f] if [:f(element):] 44 * multiple times over the the returned [Queryable] will invoke the supplied
45 * returns true. 45 * function [f] multiple times on the same element.
46 */ 46 */
47 Collection<E> where(bool f(E element)) { 47 Iterable<E> where(bool f(E element)) => new FilteredIterable<E>(this, f);
48 // TODO(floitsch): this is just a temporary function to provide a complete
49 // skeleton. It will be changed to become lazy soon.
50 List result = <E>[];
51 for (E element in this) if (f(element)) result.add(element);
52 return result;
53 }
54 48
55 /** 49 /**
56 * Check whether the collection contains an element equal to [element]. 50 * Check whether the collection contains an element equal to [element].
57 */ 51 */
58 bool contains(E element) { 52 bool contains(E element) {
59 for (E e in this) { 53 for (E e in this) {
60 if (e == element) return true; 54 if (e == element) return true;
61 } 55 }
62 return false; 56 return false;
63 } 57 }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 bool get isEmpty => !iterator.moveNext(); 126 bool get isEmpty => !iterator.moveNext();
133 } 127 }
134 128
135 class MappedIterable<E> extends Iterable<E> { 129 class MappedIterable<E> extends Iterable<E> {
136 final Iterable _iterable; 130 final Iterable _iterable;
137 final Function _f; 131 final Function _f;
138 132
139 MappedIterable(this._iterable, E this._f(element)); 133 MappedIterable(this._iterable, E this._f(element));
140 134
141 Iterator<E> get iterator => new MappedIterator<E>(_iterable.iterator, _f); 135 Iterator<E> get iterator => new MappedIterator<E>(_iterable.iterator, _f);
136
137 // Length related functions are independent of the mapping.
138 int get length => _iterable.length;
139 bool get isEmpty => _iterable.isEmpty;
142 } 140 }
143 141
144 class MappedIterator<E> extends Iterator<E> { 142 class MappedIterator<E> extends Iterator<E> {
145 E _current; 143 E _current;
146 Iterator _iterator; 144 Iterator _iterator;
147 Function _f; 145 Function _f;
148 146
149 MappedIterator(this._iterator, E this._f(element)); 147 MappedIterator(this._iterator, E this._f(element));
150 148
151 bool moveNext() { 149 bool moveNext() {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 _current = _iterator.current; 181 _current = _iterator.current;
184 return true; 182 return true;
185 } 183 }
186 } 184 }
187 _current = null; 185 _current = null;
188 return false; 186 return false;
189 } 187 }
190 188
191 E get current => _current; 189 E get current => _current;
192 } 190 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698