Enumerable<T>.generate constructor

Enumerable<T>.generate(int count, Generator<T> generator, { bool useCache: false })

Creates an enumerable of count length, where each element is generated from a given Generator function.

A convenience factory to create an enumerable of a given count length. On generation, every element is created by calling generator, passing in the index of that element.

Optionally takes a boolean useCache value. If true, the returned enumerable will contain a buffer with the calculated values. Otherwise, the enumerable will calculate the values every time it is iterated. (useCache defaults to false.)

Implementation

factory Enumerable.generate(int count, Generator<T> generator,
    {bool useCache = false}) {
  RangeError.checkNotNegative(count);
  if (count == 0) return Enumerable<T>.empty();
  if (useCache) return GeneratedEnumerable.withCache(count, generator);
  return GeneratedEnumerable(count, generator);
}