OLD | NEW |
1 // Copyright 2013 Google Inc. All Rights Reserved. | 1 // Copyright 2013 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 /** | 17 /// Returns an infinite [Iterable] of [num]s, starting from [start] and |
18 * Returns an infinite [Iterable] of [num]s, starting from [start] and | 18 /// increasing by [step]. |
19 * increasing by [step]. | |
20 */ | |
21 Iterable<num> count([num start = 0, num step = 1]) => new _Count(start, step); | 19 Iterable<num> count([num start = 0, num step = 1]) => new _Count(start, step); |
22 | 20 |
23 class _Count extends InfiniteIterable<num> { | 21 class _Count extends InfiniteIterable<num> { |
24 final num start, step; | 22 final num start, step; |
25 | 23 |
26 _Count(num this.start, num this.step); | 24 _Count(num this.start, num this.step); |
27 | 25 |
28 Iterator<num> get iterator => new _CountIterator(start, step); | 26 Iterator<num> get iterator => new _CountIterator(start, step); |
29 | 27 |
30 // TODO(justin): return an infinite list for toList() and a special Set | 28 // TODO(justin): return an infinite list for toList() and a special Set |
31 // implmentation for toSet()? | 29 // implmentation for toSet()? |
32 } | 30 } |
33 | 31 |
34 class _CountIterator implements Iterator<num> { | 32 class _CountIterator implements Iterator<num> { |
35 final num _start, _step; | 33 final num _start, _step; |
36 num _current; | 34 num _current; |
37 | 35 |
38 _CountIterator(num this._start, this._step); | 36 _CountIterator(num this._start, this._step); |
39 | 37 |
40 num get current => _current; | 38 num get current => _current; |
41 | 39 |
42 bool moveNext() { | 40 bool moveNext() { |
43 _current = (_current == null) ? _start : _current + _step; | 41 _current = (_current == null) ? _start : _current + _step; |
44 return true; | 42 return true; |
45 } | 43 } |
46 } | 44 } |
OLD | NEW |