Function reduce

  • Standard reducer for the iterable, extended for fully async syntax + iteration state.

    Below is an example of calculating the average from a sequence of numbers:

    import {pipe, reduce} from 'iter-ops';

    const input = [3, 0, -2, 5, 9, 4];

    const i = pipe(input, reduce((p, c, idx, state) => {
    state.sum = (state.sum ?? p) + c;
    return p && state.sum / (idx + 1);
    }));

    console.log(...i); //=> 3.1666(6)

    Type Parameters

    • T

    • R = T

    Parameters

    • cb: ((previousValue, currentValue, index, state) => R | Promise<R>)
        • (previousValue, currentValue, index, state): R | Promise<R>
        • Parameters

          Returns R | Promise<R>

    • Optional initialValue: R | Promise<R>

    Returns Operation<T, R>