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

    Function avg

    • Creates a sink averaging the items of the feed.

      The mean is computed from the running total and the item count, so averaging stays within constant memory whatever the size of the feed, but never completes on an infinite source. number items are averaged 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.

      bigint means are rounded to the nearest integer, halves away from zero, as no fractional bigint can carry the remainder; callers needing another rounding, or a fractional mean, combine sum with count themselves.

      An empty feed resolves to undefined, as a mean over no items is not defined; callers wanting a default supply it with ?? 0 or ?? 0n.

      Type Parameters

      • V extends number | bigint

        The type of items in the feed

      Returns Sink<V, V | undefined>

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

      If the feed mixes number and bigint items

      await pipe(
      (items(1, 2, 3, 4))
      (avg())
      ); // 2.5

      await pipe(
      (items(1n, 2n, 4n))
      (avg())
      ); // 2n

      await pipe(
      (items<number>())
      (avg())
      ); // undefined