Iterable.generate factory constructor

Iterable.generate(int count, [ E generator(int index) ])

Creates an Iterable which generates its elements dynamically.

The generated iterable has count elements, and the element at index n is computed by calling generator(n). Values are not cached, so each iteration computes the values again.

If generator is omitted, it defaults to an identity function on integers (int x) => x, so it may only be omitted if the type parameter allows integer values. That is, if E is one of int, num, Object or dynamic.

As an Iterable, new Iterable.generate(n, generator)) is equivalent to const [0, ..., n - 1].map(generator).

Source

factory Iterable.generate(int count, [E generator(int index)]) {
  if (count <= 0) return new EmptyIterable<E>();
  return new _GeneratorIterable<E>(count, generator);
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/1.24.3/dart-core/Iterable/Iterable.generate.html