ramda-extension
Version:
Helpful functions built on top of the mighty Ramda
36 lines (35 loc) • 1.28 kB
JavaScript
import { converge, reduce, nthArg, partialRight, unapply, tail } from 'ramda';
import headArg from './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 = /*#__PURE__*/converge(reduce, [/*#__PURE__*/converge(partialRight, [headArg,
/*#__PURE__*/
// iteratorFn
unapply(tail) // [accumulator, list]
]), /*#__PURE__*/nthArg(1),
/*#__PURE__*/
// accumulator
nthArg(2) // list
]);
export default reduceSource;