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

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: 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 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<E, dynamic>(this, f); 35 Iterable mappedBy(f(E element)) => new MappedIterable<E, dynamic>(this, f);
36 36
37 /** 37 /**
38 * Returns a collection with the elements of this collection 38 * Returns a lazy [Iterable] with all elements that satisfy the
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 [Iterable] 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 [Iterable] 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 WhereIterable<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<S, T> extends Iterable<T> { 129 class MappedIterable<S, T> extends Iterable<T> {
136 final Iterable<S> _iterable; 130 final Iterable<S> _iterable;
137 final Function _f; 131 final Function _f;
138 132
139 MappedIterable(this._iterable, T this._f(S element)); 133 MappedIterable(this._iterable, T this._f(S element));
140 134
141 Iterator<T> get iterator => new MappedIterator<S, T>(_iterable.iterator, _f); 135 Iterator<T> get iterator => new MappedIterator<S, T>(_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<S, T> extends Iterator<T> { 142 class MappedIterator<S, T> extends Iterator<T> {
145 T _current; 143 T _current;
146 Iterator<S> _iterator; 144 Iterator<S> _iterator;
147 Function _f; 145 Function _f;
148 146
149 MappedIterator(this._iterator, T this._f(S element)); 147 MappedIterator(this._iterator, T this._f(S element));
150 148
151 bool moveNext() { 149 bool moveNext() {
152 if (_iterator.moveNext()) { 150 if (_iterator.moveNext()) {
153 _current = _f(_iterator.current); 151 _current = _f(_iterator.current);
154 return true; 152 return true;
155 } else { 153 } else {
156 _current = null; 154 _current = null;
157 return false; 155 return false;
158 } 156 }
159 } 157 }
160 158
161 T get current => _current; 159 T get current => _current;
162 } 160 }
163 161
164 class FilteredIterable<E> extends Iterable<E> { 162 class WhereIterable<E> extends Iterable<E> {
165 final Iterable<E> _iterable; 163 final Iterable<E> _iterable;
166 final Function _f; 164 final Function _f;
167 165
168 FilteredIterable(this._iterable, bool this._f(E element)); 166 WhereIterable(this._iterable, bool this._f(E element));
169 167
170 Iterator<E> get iterator => new FilteredIterator<E>(_iterable.iterator, _f); 168 Iterator<E> get iterator => new WhereIterator<E>(_iterable.iterator, _f);
171 } 169 }
172 170
173 class FilteredIterator<E> extends Iterator<E> { 171 class WhereIterator<E> extends Iterator<E> {
174 E _current; 172 E _current;
175 Iterator _iterator; 173 Iterator _iterator;
176 Function _f; 174 Function _f;
177 175
178 FilteredIterator(this._iterator, bool this._f(E element)); 176 WhereIterator(this._iterator, bool this._f(E element));
179 177
180 bool moveNext() { 178 bool moveNext() {
181 while (_iterator.moveNext()) { 179 while (_iterator.moveNext()) {
182 if (_f(_iterator.current)) { 180 if (_f(_iterator.current)) {
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