Skip to content

meanBy() function

各要素に f を適用した値の平均を返します。空の iterable の場合は NaN を返します。

Signature:

typescript
function meanBy<A>(f: (a: A) => number, iterable: Iterable<A>): number;
function meanBy<A>(
  f: (a: A) => number,
  iterable: AsyncIterable<A>,
): Promise<number>;

Example

ts
meanBy(
  (a) => a.score,
  [
    { name: "a", score: 80 },
    { name: "b", score: 100 },
  ],
); // 90

meanBy((a) => a.score, []); // NaN

pipe(
  [{ score: 80 }, { score: 100 }],
  meanBy((a) => a.score),
); // 90

Open Source Code

Released under the Apache-2.0 License.