| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 /** | |
| 18 * Returns an [Iterable] sequence of [num]s. | |
| 19 * | |
| 20 * If only one argument is provided, [start_or_stop] is the upper bound for the | |
| 21 * sequence. If two or more arguments are provided, [stop] is the upper bound. | |
| 22 * | |
| 23 * The sequence starts at 0 if one argument is provided, or [start_or_stop] if | |
| 24 * two or more arguments are provided. The sequence increments by 1, or [step] | |
| 25 * if provided. [step] can be negative, in which case the sequence counts down | |
| 26 * from the starting point and [stop] must be less than the starting point so | |
| 27 * that it becomes the lower bound. | |
| 28 */ | |
| 29 Iterable<num> range(num start_or_stop, [num stop, num step]) => | |
| 30 new _Range(start_or_stop, stop, step); | |
| 31 | |
| 32 class _Range extends IterableBase<num> { | |
| 33 final num start, stop, step; | |
| 34 | |
| 35 _Range(num start_or_stop, num _stop, num _step) | |
| 36 : start = (_stop == null) ? 0 : start_or_stop, | |
| 37 stop = (_stop == null) ? start_or_stop : _stop, | |
| 38 step = (_step == null) ? 1 : _step { | |
| 39 if (step == 0) { | |
| 40 throw new ArgumentError("step cannot be 0"); | |
| 41 } | |
| 42 if ((step > 0) && (stop < start)) { | |
| 43 throw new ArgumentError("if step is positive," | |
| 44 " stop must be greater than start"); | |
| 45 } | |
| 46 if ((step < 0) && (stop > start)) { | |
| 47 throw new ArgumentError("if step is negative," | |
| 48 " stop must be less than start"); | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 Iterator<num> get iterator => new _RangeIterator(start, stop, step); | |
| 53 } | |
| 54 | |
| 55 class _RangeIterator implements Iterator<num> { | |
| 56 final num _stop, _step; | |
| 57 num _value; | |
| 58 bool _hasNext; | |
| 59 bool _inRange; | |
| 60 | |
| 61 _RangeIterator(num start, num stop, this._step) | |
| 62 : _value = start, | |
| 63 _stop = stop, | |
| 64 _hasNext = true, | |
| 65 _inRange = false; | |
| 66 | |
| 67 num get current => _inRange ? _value : null; | |
| 68 | |
| 69 bool moveNext() { | |
| 70 if (_hasNext && _inRange) _value += _step; | |
| 71 _inRange = _hasNext = (_step > 0) ? (_value < _stop) : (_value > _stop); | |
| 72 return _hasNext; | |
| 73 } | |
| 74 } | |
| OLD | NEW |