UNPKG

@lou.codes/iterables

Version:
23 lines (22 loc) 558 B
import { forEach } from "./forEach.js"; /** * Reducer function for iterables. * * @category Reducers * @example * ```typescript * const sum = reduce<number>(item => total => total + item); * const sumFrom0 = sum(0); * * sumFrom0([1, 2, 3]); // 6 * ``` * @param reducer Reducer function. * @returns Curried function with `reducer` in context. */ export const reduce = reducer => initialValue => iterable => { let accumulator = initialValue; return ( forEach(item => (accumulator = reducer(item)(accumulator)))(iterable), accumulator ); };