import { identity } from '../util/identity.js'; import { MathOptions } from './mathoptions.js'; /** * Computes the average of the iterable sequence. * * @param {Iterable} source The source iterable sequence to compute the average. * @param {MathOptions} [options] The options for calculating the average. * @returns {number} The computed average for the iterable sequence. */ export function average(source: Iterable, options?: MathOptions): number; /** * Computes the average of the iterable sequence. * * @template T The type of elements in the source sequence. * @param {Iterable} source The source iterable sequence to compute the average. * @param {MathOptions} [options] The options for calculating the average. * @returns {number} The computed average for the iterable sequence. */ export function average(source: Iterable, options?: MathOptions): number; /** * Computes the average of the iterable sequence. * * @param {Iterable} source The source iterable sequence to compute the average. * @param {MathOptions} [options] The options for calculating the average. * @returns {number} The computed average for the iterable sequence. */ export function average(source: Iterable, options?: MathOptions): number { const { ['selector']: selector = identity, ['thisArg']: thisArg } = options || {}; let sum = 0; let count = 0; for (const item of source) { sum += selector.call(thisArg, item); count++; } if (count === 0) { throw new Error('Empty collection'); } return sum / count; }