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

Side by Side Diff: packages/quiver_iterables/lib/src/generating_iterable.dart

Issue 2989763002: Update charted to 0.4.8 and roll (Closed)
Patch Set: Removed Cutch from list of reviewers Created 3 years, 4 months 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
OLDNEW
(Empty)
1 // Copyright 2014 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 part of quiver.iterables;
16
17 Iterable generate(initial(), next(o)) => new GeneratingIterable(initial, next);
18
19 /// 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 ///
22 /// The class is useful for creating lazy iterables from object hierarchies and
23 /// graphs.
24 ///
25 /// 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 /// expect a finite sequence, like `length` or `last`, will cause an infinite
28 /// loop.
29 ///
30 /// Example:
31 ///
32 /// class Node {
33 /// Node parent;
34 ///
35 /// /// An iterable of node and all ancestors up to the root.
36 /// Iterable<Node> ancestors =
37 /// new GeneratingIterable<Node>(() => this, (n) => n.parent);
38 ///
39 /// /// An iterable of the root and the path of nodes to this. The
40 /// /// reverse of ancestors.
41 /// Iterable<Node> path = ancestors.toList().reversed();
42 /// }
43 ///
44 class GeneratingIterable<T> extends IterableBase<T> {
45 final initial;
46 final next;
47
48 GeneratingIterable(T this.initial(), T this.next(T o));
49
50 @override
51 Iterator<T> get iterator => new _GeneratingIterator(initial(), next);
52 }
53
54 class _GeneratingIterator<T> implements Iterator<T> {
55 final next;
56 T object;
57 bool started = false;
58
59 _GeneratingIterator(T this.object, T this.next(T o));
60
61 @override
62 T get current => started ? object : null;
63
64 @override
65 bool moveNext() {
66 if (object == null) return false;
67 if (started) {
68 object = next(object);
69 } else {
70 started = true;
71 }
72 return object != null;
73 }
74 }
OLDNEW
« no previous file with comments | « packages/quiver_iterables/lib/src/enumerate.dart ('k') | packages/quiver_iterables/lib/src/infinite_iterable.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698