OLD | NEW |
1 // Copyright 2014 Google Inc. All Rights Reserved. | 1 // Copyright 2014 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 part of quiver.iterables; | 15 part of quiver.iterables; |
16 | 16 |
17 Iterable generate(initial(), next(o)) => new GeneratingIterable(initial, next); | 17 Iterable generate(initial(), next(o)) => new GeneratingIterable(initial, next); |
18 | 18 |
19 /** | 19 /// An Iterable who's first value is [object] and who's subsequent values are |
20 * An Iterable who's first value is [object] and who's subsequent values are | 20 /// generated by passing the current value to the [next] function. |
21 * generated by passing the current value to the [next] function. | 21 /// |
22 * | 22 /// The class is useful for creating lazy iterables from object hierarchies and |
23 * The class is useful for creating lazy iterables from object hierarchies and | 23 /// graphs. |
24 * graphs. | 24 /// |
25 * | 25 /// It's important that for the given initial value and next function that the |
26 * It's important that for the given initial value and next function that the | 26 /// sequence of items eventually terminates. Otherwise calling methods that |
27 * sequence of items eventually terminates. Otherwise calling methods that | 27 /// expect a finite sequence, like `length` or `last`, will cause an infinite |
28 * expect a finite sequence, like `length` or `last`, will cause an infinite | 28 /// loop. |
29 * loop. | 29 /// |
30 * | 30 /// Example: |
31 * Example: | 31 /// |
32 * | 32 /// class Node { |
33 * class Node { | 33 /// Node parent; |
34 * Node parent; | 34 /// |
35 * | 35 /// /// An iterable of node and all ancestors up to the root. |
36 * /** | 36 /// Iterable<Node> ancestors = |
37 * * An iterable of node and all ancestors up to the root. | 37 /// new GeneratingIterable<Node>(() => this, (n) => n.parent); |
38 * */ | 38 /// |
39 * Iterable<Node> ancestors = | 39 /// /// An iterable of the root and the path of nodes to this. The |
40 * new GeneratingIterable<Node>(() => this, (n) => n.parent); | 40 /// /// reverse of ancestors. |
41 * | 41 /// Iterable<Node> path = ancestors.toList().reversed(); |
42 * /** | 42 /// } |
43 * * An iterable of the root and the path of nodes to this. The reverse | 43 /// |
44 * * of ancestors. | |
45 * */ | |
46 * Iterable<Node> path = ancestors.toList().reversed(); | |
47 * } | |
48 * | |
49 */ | |
50 class GeneratingIterable<T> extends IterableBase<T> { | 44 class GeneratingIterable<T> extends IterableBase<T> { |
51 final initial; | 45 final initial; |
52 final next; | 46 final next; |
53 | 47 |
54 GeneratingIterable(T this.initial(), T this.next(T o)); | 48 GeneratingIterable(T this.initial(), T this.next(T o)); |
55 | 49 |
56 @override | 50 @override |
57 Iterator<T> get iterator => new _GeneratingIterator(initial(), next); | 51 Iterator<T> get iterator => new _GeneratingIterator(initial(), next); |
58 } | 52 } |
59 | 53 |
(...skipping 11 matching lines...) Expand all Loading... |
71 bool moveNext() { | 65 bool moveNext() { |
72 if (object == null) return false; | 66 if (object == null) return false; |
73 if (started) { | 67 if (started) { |
74 object = next(object); | 68 object = next(object); |
75 } else { | 69 } else { |
76 started = true; | 70 started = true; |
77 } | 71 } |
78 return object != null; | 72 return object != null; |
79 } | 73 } |
80 } | 74 } |
OLD | NEW |