このページはまだ翻訳されていないため、英語で表示されます。 翻訳に参加する

average

The mean of every number the pipeline produces — NaN when there are none.

double average(Iterable<num> iterable) Future<double> averageAsync(FxAsyncIterable<num> iterable) double Fx<num>.average() // chain (sync) Future<double> FxAsync<num>.average() // chain (async)

Lecture

average is a terminal operator that walks the whole pipeline once, tracking both a running total and a running count, then divides. It always returns a double, even for a list of plain ints.

The one behavior worth internalizing: average of an empty iterable is double.nan — it's computing 0 / 0, not throwing and not defaulting to 0. This mirrors FxTS exactly. If your pipeline might be empty, check for that explicitly (result.isNaN) rather than assuming a numeric fallback.

Like sum, the chain form is exposed through a Fx<num> / FxAsync<num> extension, so it only becomes available once the compiler can see your chain's element type is numeric.

Demo 1 · Basics & the empty case

Demo 2 · Async

Try it yourself

Exercise: average only the passing scores (>= 60).

Related: sum — the numerator this divides · size — the denominator this divides by · min · max — the other numeric terminals