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

    Function sum

    • Creates a sink summing the items of the feed.

      Items are added in source order, seeding the total with the first one. number items are added as IEEE 754 doubles and bigint items as exact integers: a feed is expected to carry one numeric type throughout, and mixing the two is reported rather than silently coerced.

      An empty feed resolves to undefined; callers wanting the additive identity supply it with ?? 0 or ?? 0n.

      Warning

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

      Type Parameters

      • V extends number | bigint

        The type of items in the feed

      Returns Sink<V, V | undefined>

      A sink resolving to the sum of the items of the feed, or to undefined if the feed carried no items

      TypeError If the feed mixes number and bigint items

      await pipe(
      (items([1, 2, 3, 4, 5]))
      (sum())
      ); // 15

      await pipe(
      (items([1n, 2n, 3n]))
      (sum())
      ); // 6n

      await pipe(
      (items<number>([]))
      (sum())
      ) ?? 0; // 0