range function

Iterable<num> range (num start_or_stop, [ num stop num step ])

Returns an Iterable sequence of nums.

If only one argument is provided, start_or_stop is the upper bound for the sequence. If two or more arguments are provided, stop is the upper bound.

The sequence starts at 0 if one argument is provided, or start_or_stop if two or more arguments are provided. The sequence increments by 1, or step if provided. step can be negative, in which case the sequence counts down from the starting point and stop must be less than the starting point so that it becomes the lower bound.

Implementation

Iterable<num> range(num start_or_stop, [num stop, num step]) sync* {
  final start = stop == null ? 0 : start_or_stop;
  stop ??= start_or_stop;
  step ??= 1;

  if (step == 0) throw new ArgumentError('step cannot be 0');
  if (step > 0 && stop < start)
    throw new ArgumentError('if step is positive,'
        ' stop must be greater than start');
  if (step < 0 && stop > start)
    throw new ArgumentError('if step is negative,'
        ' stop must be less than start');

  for (num value = start; step < 0 ? value > stop : value < stop; value += step)
    yield value;
}