iterate<U> static method

IList<U> iterate<U>(
  1. U base,
  2. int count,
  3. Op<U> op
)

Apply Op on previous state of base and return all results

Implementation

static IList<U> iterate<U>(U base, int count, Op<U> op) {
  IList<U> iterations() {
    final l = List.filled(count, base, growable: false);
    U acc = base;
    int i = 1;
    l[0] = acc;

    while (i < count) {
      acc = op(acc);
      l[i] = acc;
      i += 1;
    }

    return l.lock;
  }

  return count > 0 ? iterations() : <U>[].lock;
}