iteragain
Version:
Javascript Iterable/Iterator/Generator-function utilities.
24 lines • 834 B
JavaScript
import { REDUCE_EMPTY_ERROR } from './internal/emptyIteratorError';
import toIterator from './toIterator';
export function reduce(...args) {
if (typeof args[0] === 'function')
return it => reduce(it, args[0], args[1]);
const it = toIterator(args[0]);
let next;
const reducer = args[1];
if (args[2] === undefined) {
const first = it.next();
if (first.done)
throw new TypeError(REDUCE_EMPTY_ERROR);
let accumulator = first.value;
while (!(next = it.next()).done)
accumulator = reducer(accumulator, next.value);
return accumulator;
}
let accumulator = args[2];
while (!(next = it.next()).done)
accumulator = reducer(accumulator, next.value);
return accumulator;
}
export default reduce;
//# sourceMappingURL=reduce.js.map