import { identity } from '../util/identity.js'; import { MathOptions } from './mathoptions.js'; /** * Computes the sum of a sequence of values. * * @param {AsyncIterable} source A sequence of values to calculate the sum. * @param {MathOptions} [options] Optional options for providing a selector, thisArg and abort signal. * @returns {Promise} A promise containing the sum of the sequence of values. */ export function sum(source: Iterable, options?: MathOptions): number; /** * Computes the sum of a sequence of values. * * @template T The type of values in the source sequence. * @param {Iterable} source A sequence of values to calculate the sum. * @param {MathOptions} [options] Optional options for providing a selector, thisArg and abort signal. * @returns {Promise} A promise containing the sum of the sequence of values. */ export function sum(source: Iterable, options?: MathOptions): number; /** * Computes the sum of a sequence of values. * * @param {Iterable} source A sequence of values to calculate the sum. * @param {MathOptions} [options] Optional options for providing a selector, thisArg and abort signal. * @returns {Promise} A promise containing the sum of the sequence of values. */ export function sum(source: Iterable, options?: MathOptions): number { const { ['selector']: selector = identity, ['thisArg']: thisArg } = options || {}; let value = 0; for (const item of source) { value += selector!.call(thisArg, item); } return value; }