IntRange.linspace constructor

IntRange.linspace(
  1. int start,
  2. int stop,
  3. int count
)

Returns an iterable of count integers from start inclusive to stop inclusive.

print(IntRange.linspace(1, 10, 5)); => (1, 3, 5, 7, 9)

Implementation

factory IntRange.linspace(int start, int stop, int count) {
  if (count <= 0) {
    throw ArgumentError.value(count, 'count', 'Must be a positive integer');
  } else if(count == 1) {
    return IntRange(start, stop, stop - start);
  }

  int step = 0;
  if (stop > start) {
    step = (stop - start) ~/ (count - 1);
  } else {
    step = (start - stop) ~/ (count - 1);
  }

  if (step == 0) step = 1;
  if (stop < start) {
    step = -step;
  }

  stop = start + (step * (count - 1));
  return IntRange(start, stop, step);
}