@arrows/array
Version:
Functional tools for JS arrays
36 lines (35 loc) • 1.28 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.reduce = void 0;
const curry_1 = require("@arrows/composition/curry");
const _reduceFirst = (reducingFn, arr) => arr.reduce(reducingFn);
const reduceFirst = curry_1.default(_reduceFirst);
const _reduce = (reducingFn, initialValue, arr) => arr.reduce(reducingFn, initialValue);
const curriedReduce = curry_1.default(_reduce);
/**
* Functional wrapper for Array.prototype.reduce
*
* Calls the specified reducing function for all the elements in an array.
* The return value of the reducing function is the accumulated result,
* and is provided as an argument in the next call to the reducing function.
*
* @param reducingFn Reducing function
* @param initialValue Initial value of the accumulator
* @param arr Initial array
* @returns Final accumulator value
*
* @method first Reduce without initializer
*/
const reduce = Object.assign(curriedReduce, {
/**
* Reduce without initializer.
* The first element of the array will be used as an initial accumulator.
*
* @param reducingFn Reducing function
* @param arr Initial array
* @returns Final accumulator value
*/
first: reduceFirst,
});
exports.reduce = reduce;
exports.default = reduce;
;