Skip to content

@epikodelabs/streamix


@epikodelabs/streamix / aggregates/src/public-api / average

Function: average()

average<T>(selector?): Operator<T, number>

Defined in: projects/libraries/streamix/aggregates/src/lib/operators/average.ts:16

Creates a stream operator that computes the arithmetic mean of values from the source stream.

The operator consumes every value, optionally maps it through the provided selector, keeps running totals and counts, and emits the average once the source stream completes. When the source produces no values, it still completes and emits 0.

Type Parameters

T

T = any

The type of the values in the source stream.

Parameters

selector?

(value, index) => MaybePromise<number>

Optional function that projects each value to a number; it receives the value and its index and may return a promise. Defaults to interpreting the value itself as a number.

Returns

Operator<T, number>

An Operator instance that can be used in a stream's pipe method. The operator emits exactly one numeric value before completing every subscription.

Released under the GNU AGPL v3 or later.