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

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: Update comments. 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/collection.dart ('k') | no next file » | 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> {
Anders Johnsen 2012/11/06 17:51:47 I think this would be a good time to introduce I
18 /** 13 /**
19 * Returns an [Iterator] that iterates over this [Iterable] object. 14 * Returns an [Iterator] that iterates over this [Iterable] object.
20 */ 15 */
21 Iterator<E> iterator(); 16 Iterator<E> iterator();
17
18 /**
19 * Returns a lazy [Iterable] where each element [:e:] of [this] is replaced
20 * by the result of [:f(e):].
21 *
22 * This method returns a view of the mapped elements. As long as the
23 * returned [Iterable] is not iterated over, the supplied function [f] will
24 * not be invoked. The transformed elements will not be cached. Iterating
25 * multiple times over the the returned [Iterable] will invoke the supplied
26 * function [f] multiple times on the same element.
27 */
28 Iterable map(f(E element));
29
30 /**
31 * Returns a lazy [Iterable] with all elements [:e:] that satisfy the
32 * predicate [f].
33 *
34 * This method returns a view of the mapped elements. As long as the
35 * returned [Iterable] is not iterated over, the supplied function [f] will
36 * not be invoked. Iterating will not cache results, and thus iterating
37 * multiple times over the the returned [Iterable] will invoke the supplied
38 * function [f] multiple times on the same element.
39 */
40 Iterable<E> filter(bool f(E element));
41
42 /**
43 * Checks whether [this] contains an element equal to [element] by
44 * iterating over it.
45 */
46 bool contains(E element) {
47 for (E e in this) {
48 if (e == element) return true;
49 }
50 return false;
51 }
52
53 /**
54 * Applies the function [f] to each element of [this] by iterating over it.
55 */
56 void forEach(void f(E element)) {
57 for (E element in this) f(element);
58 }
59
60 /**
61 * Reduces this [Iterable] to a single value by iterating over [this] and
62 * iteratively combining each element of the collection with an existing
63 * value using the provided function.
64 *
65 * Uses [initialValue] as the initial value, and the function [combine] to
66 * create a new value from the previous one and an element.
67 *
68 * Example of calculating the sum of a collection:
69 *
70 * collection.reduce(0, (prev, element) => prev + element);
71 */
72 Dynamic reduce(var initialValue,
73 Dynamic combine(var previousValue, E element)) {
Mads Ager (google) 2012/11/07 07:13:19 Dynamic -> dynamic
74 var value = initialValue;
75 for (E element in this) value = combine(value, element);
76 return value;
77 }
78
79 /**
80 * Returns true if every element of [this] satisifies the predicate [f].
81 * Returns false otherwise.
82 *
83 * The iteration of [this] is stopped as soon as one non-satisfying element is
84 * found.
85 */
86 bool every(bool f(E element)) {
87 for (E element in this) {
88 if (!f(element)) return false;
89 }
90 return true;
91 }
92
93 /**
94 * Returns true if at least one element of [this] satisfies the predicate [f].
95 * Returns false otherwise.
96 *
97 * The iteration of [this] is stopped as soon as one satisfying element is
98 * found.
99 */
100 bool any(bool f(E element)) {
101 for (E element in this) {
102 if (f(element)) return true;
103 }
104 return false;
105 }
106
107 /**
108 * Returns true if there is no element in [this].
109 */
110 bool get isEmpty => !iterator().hasNext;
22 } 111 }
OLDNEW
« no previous file with comments | « sdk/lib/core/collection.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698