UNPKG

1.32 kBTypeScriptView Raw
1/**
2 * Summarize all values in an iterable using a reducer function.
3 *
4 * @param object - The iterable object of interest.
5 *
6 * @param fn - The reducer function to invoke for each value.
7 *
8 * @param initial - The initial value to start accumulation.
9 *
10 * @returns The final accumulated value.
11 *
12 * #### Notes
13 * The `reduce` function follows the conventions of `Array#reduce`.
14 *
15 * If the iterator is empty, an initial value is required. That value
16 * will be used as the return value. If no initial value is provided,
17 * an error will be thrown.
18 *
19 * If the iterator contains a single item and no initial value is
20 * provided, the single item is used as the return value.
21 *
22 * Otherwise, the reducer is invoked for each element in the iterable.
23 * If an initial value is not provided, the first element will be used
24 * as the initial accumulated value.
25 *
26 * #### Complexity
27 * Linear.
28 *
29 * #### Example
30 * ```typescript
31 * import { reduce } from '@lumino/algorithm';
32 *
33 * let data = [1, 2, 3, 4, 5];
34 *
35 * let sum = reduce(data, (a, value) => a + value); // 15
36 * ```
37 */
38export declare function reduce<T>(object: Iterable<T>, fn: (accumulator: T, value: T, index: number) => T): T;
39export declare function reduce<T, U>(object: Iterable<T>, fn: (accumulator: U, value: T, index: number) => U, initial: U): U;