yta
    Preparing search index...

    Function accumulate

    • Map the iterator with a function that can access the previous result. Works similar to reduce except that instead of returning a single value, we yield on every iteration and return the new iterator.

      import { pipe } from "yta";
      import { accumulate, range, toArray } from "yta/sync";

      pipe(
      range(1, 6),
      accumulate((sum, item) => sum + item, 0),
      toArray(),
      );
      // => [1, 3, 6, 10, 15]

      You can use this to create pair wise accumulation of previous and current items.

      import { pipe } from "yta";
      import { accumulate, drop, range, toArray } from "yta/sync";

      pipe(
      range(1, 6),
      accumulate(([, last], current) => [last, current], [0, 0]),
      drop(1),
      toArray(),
      );
      // => [[1, 2], [2, 3], [3, 4], [4, 5]]

      See also:

      Type Parameters

      • A

        The input item type

      • B

        The output item type

      Parameters

      • fn: (acc: B, item: A) => B

        The accumulator function

      • init: B

        The first item passed to the accumulator function

      Returns (items: Iterable<A>) => Generator<B, void>