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 typedef T _Initial<T>(); |
| 18 typedef T _Next<T>(T value); |
| 19 |
17 Iterable generate(initial(), next(o)) => new GeneratingIterable(initial, next); | 20 Iterable generate(initial(), next(o)) => new GeneratingIterable(initial, next); |
18 | 21 |
19 /** | 22 /// 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 | 23 /// generated by passing the current value to the [next] function. |
21 * generated by passing the current value to the [next] function. | 24 /// |
22 * | 25 /// 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 | 26 /// graphs. |
24 * graphs. | 27 /// |
25 * | 28 /// 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 | 29 /// sequence of items eventually terminates. Otherwise calling methods that |
27 * sequence of items eventually terminates. Otherwise calling methods that | 30 /// 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 | 31 /// loop. |
29 * loop. | 32 /// |
30 * | 33 /// Example: |
31 * Example: | 34 /// |
32 * | 35 /// class Node { |
33 * class Node { | 36 /// Node parent; |
34 * Node parent; | 37 /// |
35 * | 38 /// /// An iterable of node and all ancestors up to the root. |
36 * /** | 39 /// Iterable<Node> ancestors = |
37 * * An iterable of node and all ancestors up to the root. | 40 /// new GeneratingIterable<Node>(() => this, (n) => n.parent); |
38 * */ | 41 /// |
39 * Iterable<Node> ancestors = | 42 /// /// An iterable of the root and the path of nodes to this. The |
40 * new GeneratingIterable<Node>(() => this, (n) => n.parent); | 43 /// /// reverse of ancestors. |
41 * | 44 /// Iterable<Node> path = ancestors.toList().reversed(); |
42 * /** | 45 /// } |
43 * * An iterable of the root and the path of nodes to this. The reverse | |
44 * * of ancestors. | |
45 * */ | |
46 * Iterable<Node> path = ancestors.toList().reversed(); | |
47 * } | |
48 * | |
49 */ | |
50 class GeneratingIterable<T> extends IterableBase<T> { | 46 class GeneratingIterable<T> extends IterableBase<T> { |
51 final initial; | 47 final _Initial<T> initial; |
52 final next; | 48 final _Next<T> next; |
53 | 49 |
54 GeneratingIterable(T this.initial(), T this.next(T o)); | 50 GeneratingIterable(this.initial, this.next); |
55 | 51 |
56 @override | 52 @override |
57 Iterator<T> get iterator => new _GeneratingIterator(initial(), next); | 53 Iterator<T> get iterator => new _GeneratingIterator(initial(), next); |
58 } | 54 } |
59 | 55 |
60 class _GeneratingIterator<T> implements Iterator<T> { | 56 class _GeneratingIterator<T> implements Iterator<T> { |
61 final next; | 57 final _Next<T> next; |
62 T object; | 58 T object; |
63 bool started = false; | 59 bool started = false; |
64 | 60 |
65 _GeneratingIterator(T this.object, T this.next(T o)); | 61 _GeneratingIterator(T this.object, this.next); |
66 | 62 |
67 @override | 63 @override |
68 T get current => started ? object : null; | 64 T get current => started ? object : null; |
69 | 65 |
70 @override | 66 @override |
71 bool moveNext() { | 67 bool moveNext() { |
72 if (object == null) return false; | 68 if (object == null) return false; |
73 if (started) { | 69 if (started) { |
74 object = next(object); | 70 object = next(object); |
75 } else { | 71 } else { |
76 started = true; | 72 started = true; |
77 } | 73 } |
78 return object != null; | 74 return object != null; |
79 } | 75 } |
80 } | 76 } |
OLD | NEW |