common-stuff
    Preparing search index...

    Function reduce

    • Reduces values of an iterable (Array, Set, Iterable, AsyncIterable) into a single value using the provided reducer function.

      Works with:

      • Synchronous iterables (Array, Set, custom Iterable)
      • Asynchronous iterables (AsyncIterable / async generators)
      • Synchronous or asynchronous reducer functions (if the reducer returns a promise the result becomes a promise)

      If the initial value isn't provided, the first emitted value is used as the accumulator (same semantics as Array.prototype.reduce). When used with an empty iterable without an initial value an error is thrown.

      Type Parameters

      • I extends Iterable<unknown, any, any> | AsyncIterable<unknown, any, any>
      • R

      Parameters

      • iterable: I
      • reducer: (acc: IterableItem<I>, value: IterableItem<I>, index: number) => R

      Returns ReduceResult<I, R>

      reduce([1,2,3], (a,b) => a + b, 0) // 6
      reduce(new Set(['a','b']), (a,b) => a + b) // 'ab'
      await reduce(asyncGen(), (a,b) => a + b) // 6
    • Reduces values of an iterable (Array, Set, Iterable, AsyncIterable) into a single value using the provided reducer function.

      Works with:

      • Synchronous iterables (Array, Set, custom Iterable)
      • Asynchronous iterables (AsyncIterable / async generators)
      • Synchronous or asynchronous reducer functions (if the reducer returns a promise the result becomes a promise)

      If the initial value isn't provided, the first emitted value is used as the accumulator (same semantics as Array.prototype.reduce). When used with an empty iterable without an initial value an error is thrown.

      Type Parameters

      • I extends Iterable<unknown, any, any> | AsyncIterable<unknown, any, any>
      • R

      Parameters

      • iterable: I
      • reducer: (acc: Awaited<R>, value: IterableItem<I>, index: number) => R
      • initial: Awaited<R>

      Returns ReduceResult<I, R>

      reduce([1,2,3], (a,b) => a + b, 0) // 6
      reduce(new Set(['a','b']), (a,b) => a + b) // 'ab'
      await reduce(asyncGen(), (a,b) => a + b) // 6