@metreeca/flow - v0.9.21
    Preparing search index...

    Function reduce

    Creates a sink reducing the feed to a single value, with or without an initial value.

    • Creates a sink folding the feed into a single item.

      The first item seeds the accumulator and every following one is folded into it, in source order, so the feed is drained without retaining more than the accumulator.

      Type Parameters

      • V

        The type of items in the feed

      Parameters

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

        The function folding an item into the accumulator and returning the updated one

      Returns Sink<V, V | undefined>

      A sink resolving to the final accumulator, to the only item of a singleton feed, or to undefined for an empty feed

      await pipe(
      (items(1, 2, 3, 4, 5))
      (reduce((total, n) => total+n))
      ); // 15
    • Creates a sink folding the feed into a value of a different type.

      Folds like reduce without an initial argument, seeding the accumulator with the supplied value rather than with the first item, so the result type is free of the item type and an empty feed resolves to the seed.

      Type Parameters

      • V

        The type of items in the feed

      • R

        The type of the accumulated result

      Parameters

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

        The function folding an item into the accumulator and returning the updated one

      • initial: R

        The value seeding the accumulator

      Returns Sink<V, R>

      A sink resolving to the final accumulator

      await pipe(
      (items(1, 2, 3, 4, 5))
      (reduce((total, n) => total+n, 10))
      ); // 25