import { curryN } from "@unboxing/function"; import { CurriedFunction2 } from '@unboxing/core' export type ReduceFun = (acc: R, elem: T, index: number, arr: ArrayLike) => R; interface Reduce { (fn: ReduceFun, acc: R, list: ArrayLike): R; (fn: ReduceFun, acc: R): (list: ArrayLike) => R; (fn: ReduceFun): CurriedFunction2, R>; } /** * Returns a single item by iterating through the list, successively calling * the iterator function and passing it an accumulator value and the current * value from the array, and then passing the result to the next call. */ export const reduceArray = curryN(3, (fn: ReduceFun, acc: R, arr: ArrayLike = []) => { const len = arr.length; for (let i = 0; i < len; i++) { acc = fn(acc, arr[i], i, arr); } return acc; }) as Reduce