@reactivex/ix-esnext-esm
Version:
The Interactive Extensions for JavaScript
28 lines (26 loc) • 898 B
JavaScript
import { identity } from '../util/identity';
/**
* Computes the average of a sequence of values from the sequence either from the sequence itself
* or from the selector function.
* @example
* // Using non chained version
* const result = average([1, 2, 3]);
* const result = Ix.Iterable.of(1, 2, 3).average();
* console.log(result);
* @param {Iterable<any>} source A sequence of values to calculate the average of.
* @param {function(x: any): number} [selector] A transform function to apply to each element.
* @returns {number} The average of the sequence of values.
*/
export function average(source, selector = identity) {
let sum = 0;
let count = 0;
for (const item of source) {
sum += selector(item);
count++;
}
if (count === 0) {
throw new Error('Empty collection');
}
return sum / count;
}
//# sourceMappingURL=average.mjs.map