@mirei/ts-collections
    Preparing search index...

    Function aggregate

    • Combines the elements of the sequence by applying an accumulator to each element and optionally projecting the final result.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      • TAccumulate = TElement

        Type of the intermediate accumulator. Defaults to TElement when no seed is provided.

      • TResult = TAccumulate

        Type returned when a resultSelector is supplied.

      Parameters

      • source: Iterable<TElement>

        The source iterable.

      • accumulator: (accumulator: TAccumulate, element: TElement) => TAccumulate

        Function that merges the running accumulator with the next element.

      • Optionalseed: TAccumulate

        Optional initial accumulator value. When omitted, the first element is used as the starting accumulator.

      • OptionalresultSelector: (accumulator: TAccumulate) => TResult

        Optional projection applied to the final accumulator before it is returned.

      Returns TAccumulate | TResult

      The final accumulator (or its projection).

      Thrown when source has no elements and no seed is provided.

      The source sequence is enumerated exactly once. Supply a seed to avoid exceptions on empty sequences and to control the accumulator type.

      const numbers = [1, 2, 3, 4, 5];
      const sum = aggregate(numbers, (acc, x) => acc + x);
      console.log(sum); // 15

      const product = aggregate(numbers, (acc, x) => acc * x, 1);
      console.log(product); // 120