@metreeca/pipe - v0.9.20
    Preparing search index...

    Function reduce

    • Creates a sink reducing the stream to a single value without an initial value.

      Type Parameters

      • V

        The type of items in the stream

      Parameters

      • reducer: (accumulator: V, item: V) => V | Promise<V>

        The function to combine the accumulator with each item

      Returns Sink<V, V | undefined>

      A sink that reduces the stream to a single value, the first item for singleton streams, or undefined for empty streams

      await items([1, 2, 3, 4, 5])(reduce((acc, x) => acc + x));  // 15
      await items([42])(reduce((acc, x) => acc + x)); // 42
      await items([])(reduce((acc, x) => acc + x)); // undefined
    • Creates a sink reducing the stream to a single value with an initial value.

      Type Parameters

      • V

        The type of items in the stream

      • R

        The type of the accumulated result

      Parameters

      • reducer: (accumulator: R, item: V) => R | Promise<R>

        The function to combine the accumulator with each item

      • initial: R

        The initial value for the accumulator

      Returns Sink<V, R>

      A sink that reduces the stream to a single value

      await items([1, 2, 3, 4, 5])(reduce((acc, x) => acc + x, 0));  // 15
      await items([1, 2, 3, 4, 5])(reduce((acc, x) => acc + x, 10)); // 25
      await items([])(reduce((acc, x) => acc + x, 0)); // 0