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

Side by Side Diff: sdk/lib/core/iterable.dart

Issue 11366111: Make Iterable more powerful (and lazy). (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Fix current. was in Iterable and not 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
« no previous file with comments | « sdk/lib/core/corelib_sources.gypi ('k') | sdk/lib/core/iterator.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 groups functionality that can be expressed on top
7 * [Iterable] object. 7 * of [Iterator]s.
8 * 8 *
9 * This interface is used by the for-in construct to iterate over an 9 * [Iterable]s provide an [iterator] getter and can thus be used as the
10 * [Iterable] object. 10 * right-hand side of a for-in construct.
11 * The for-in construct takes an [Iterable] object at the right-hand
12 * side, and calls its [iterator] method to get an [Iterator] on it.
13 *
14 * A user-defined class that implements the [Iterable] interface can
15 * be used as the right-hand side of a for-in construct.
16 */ 11 */
17 abstract class Iterable<E> { 12 abstract class Iterable<E> {
13 const Iterable();
14
18 /** 15 /**
19 * Returns an [Iterator] that iterates over this [Iterable] object. 16 * Returns an [Iterator] that iterates over this [Iterable] object.
20 */ 17 */
21 Iterator<E> iterator(); 18 Iterator<E> get iterator;
19
20 /**
21 * Returns a [Queryable] view of [this].
22 */
23 Queryable<E> query();
24
25 /**
26 * Returns a lazy [Queryable] where each element [:e:] of [this] is replaced
27 * by the result of [:f(e):].
28 *
29 * This method returns a view of the mapped elements. As long as the
30 * returned [Queryable] is not iterated over, the supplied function [f] will
31 * not be invoked. The transformed elements will not be cached. Iterating
32 * multiple times over the the returned [Queryable] will invoke the supplied
33 * function [f] multiple times on the same element.
34 */
35 Queryable mappedBy(f(E element));
36
37 /**
38 * Returns a lazy [Queryable] with all elements that satisfy the
39 * predicate [f].
40 *
41 * This method returns a view of the mapped elements. As long as the
42 * returned [Queryable] is not iterated over, the supplied function [f] will
43 * not be invoked. Iterating will not cache results, and thus iterating
44 * multiple times over the the returned [Queryable] will invoke the supplied
45 * function [f] multiple times on the same element.
46 */
47 Queryable<E> where(bool f(E element));
48
49 /**
50 * Checks whether [this] contains an element equal to [element]. This
51 * operation can be slow if [this] needs to iterate over all elements.
52 */
53 bool contains(E element) {
54 for (E e in this) {
55 if (e == element) return true;
56 }
57 return false;
58 }
59
60 /**
61 * Applies the function [f] to each element of [this] by iterating over it.
62 */
63 void forEach(void f(E element)) {
64 for (E element in this) f(element);
65 }
66
67 /**
68 * Reduces this [Iterable] to a single value by iterating over [this] and
69 * iteratively combining each element of [this] with an existing
70 * value using the provided function.
71 *
72 * Uses [initialValue] as the initial value, and the function [combine] to
73 * create a new value from the previous one and an element.
74 *
75 * Example of calculating the sum of an [Iterable]:
76 *
77 * collection.reduce(0, (prev, element) => prev + element);
78 */
79 dynamic reduce(var initialValue,
80 dynamic combine(var previousValue, E element)) {
81 var value = initialValue;
82 for (E element in this) value = combine(value, element);
83 return value;
84 }
85
86 /**
87 * Returns true if every element of [this] satisifies the predicate [f].
88 * Returns false otherwise.
89 *
90 * The iteration of [this] is stopped as soon as one non-satisfying element is
91 * found.
92 */
93 bool every(bool f(E element)) {
94 for (E element in this) {
95 if (!f(element)) return false;
96 }
97 return true;
98 }
99
100 /**
101 * Returns true if at least one element of [this] satisfies the predicate [f].
102 * Returns false otherwise.
103 *
104 * The iteration of [this] is stopped as soon as one satisfying element is
105 * found.
106 */
107 bool any(bool f(E element)) {
108 for (E element in this) {
109 if (f(element)) return true;
110 }
111 return false;
112 }
113
114 /**
115 * Returns true if [this] is empty.
116 */
117 bool get isEmpty => !iterator.moveNext();
22 } 118 }
OLDNEW
« no previous file with comments | « sdk/lib/core/corelib_sources.gypi ('k') | sdk/lib/core/iterator.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698