import { identity } from '../util/identity' ; import { MathOptions } from './mathoptions' ; param { Iterable < number > } source param { MathOptions < number > } options returns { number } export function average ( source , options ? ) ; template T param { Iterable < T > } source param { MathOptions < T > } options returns { number } export function average < T > ( source , options ? ) ; param { Iterable < any > } source param { MathOptions < any > } options returns { number } export function average ( source , options ? ) { 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 ; }