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 /// Returns an [Iterable] sequence of [num]s. | |
18 /// | |
19 /// If only one argument is provided, [start_or_stop] is the upper bound for the | |
20 /// sequence. If two or more arguments are provided, [stop] is the upper bound. | |
21 /// | |
22 /// The sequence starts at 0 if one argument is provided, or [start_or_stop] if | |
23 /// two or more arguments are provided. The sequence increments by 1, or [step] | |
24 /// if provided. [step] can be negative, in which case the sequence counts down | |
25 /// from the starting point and [stop] must be less than the starting point so | |
26 /// that it becomes the lower bound. | |
27 Iterable<num> range(num start_or_stop, [num stop, num step]) { | |
28 var start = (stop == null) ? 0 : start_or_stop; | |
29 stop = (stop == null) ? start_or_stop : stop; | |
30 step = (step == null) ? 1 : step; | |
31 if (step == 0) { | |
32 throw new ArgumentError("step cannot be 0"); | |
33 } | |
34 if ((step > 0) && (stop < start)) { | |
35 throw new ArgumentError("if step is positive," | |
36 " stop must be greater than start"); | |
37 } | |
38 if ((step < 0) && (stop > start)) { | |
39 throw new ArgumentError("if step is negative," | |
40 " stop must be less than start"); | |
41 } | |
42 return _range(start, stop, step); | |
43 } | |
44 | |
45 Iterable<num> _range(num start, num stop, num step) sync* { | |
46 while (step < 0 ? start > stop : start < stop) { | |
47 yield start; | |
48 start += step; | |
49 } | |
50 } | |
OLD | NEW |