@metreeca/flow - v0.10.0
    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.

      Warning

      • Exhaustive: every item is folded before the sink resolves, so an infinite feed never completes.
      • Streaming: no more than the accumulator is held in memory, whatever the size of the feed.
      • Stateful: the accumulator covers the items drawn, so a sink closing a nested or truncated feed sees those alone.

      Type Parameters

      • V

        The type of items in the feed

      Parameters

      • reducer: (accumulator: NoInfer<V>, item: NoInfer<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.

      Warning

      • Exhaustive: every item is folded before the sink resolves, so an infinite feed never completes.
      • Streaming: no more than the accumulator is held in memory, whatever the size of the feed.
      • Stateful: the accumulator covers the items drawn, so a sink closing a nested or truncated feed sees those alone, seeded afresh with initial at each.

      Type Parameters

      • V

        The type of items in the feed

      • R

        The type of the accumulated result

      Parameters

      • reducer: (accumulator: R, item: NoInfer<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