UNPKG

ramda-extension

Version:

Helpful functions built on top of the mighty Ramda

43 lines (38 loc) 1.51 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _ramda = require("ramda"); var _headArg = _interopRequireDefault(require("./headArg")); /** * Extends the reduce functionality by adding the original accumulator value * as a third argument and the original list as a fourth argument to the * iterator function. * * @func * @category List * * @param {Function} fn The iterator function. Receives four arguments, the * accumulator, the current element, the source accumulator and the * source list. * @param {*} acc The initial accumulator value and value passed as the source * accumulator value in the iterator function. * @param {Array} list The list to iterator over and value passed as the source * list in the iterator function. * @return {*} The reduced result. * * @example * R_.reduceSource((acc, v, sAcc) => v + acc + sAcc, 1, [1, 2, 3]); // 10 * R_.reduceSource(R.pipe(R.unapply(R.flatten), R.sum), 0, [1, 2]); // 9 * * @sig ((a, b, a, [b]) -> a) -> a -> [b] -> a */ var reduceSource = (0, _ramda.converge)(_ramda.reduce, [(0, _ramda.converge)(_ramda.partialRight, [_headArg.default, // iteratorFn (0, _ramda.unapply)(_ramda.tail) // [accumulator, list] ]), (0, _ramda.nthArg)(1), // accumulator (0, _ramda.nthArg)(2) // list ]); var _default = reduceSource; exports.default = _default;