lib/foldl.js
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.foldl = foldl;
var _head = require('./head');
/**
* Applies a function against an accumulator and each value of the list from left-to-right
* to reduce it to a single value. (also known variously as reduce).
*
* @since v0.1.0
* @param {Function} fn - The accumlating function, the function to
* execute on each value in the array
* @param {Array} list - The list we should accumlate
* @param {any} accumlator -
* @param {number} index -
* @return {any}
* @example
* const add = (prev, current) => prev + current
* foldl(add)([1, 2, 3]) // => 6
*/
function foldl(fn) {
return function (list) {
var accumulator = arguments.length <= 1 || arguments[1] === undefined ? (0, _head.head)(list) : arguments[1];
var index = arguments.length <= 2 || arguments[2] === undefined ? 0 : arguments[2];
var result = void 0;
if (index === list.length - 1) {
result = accumulator;
} else {
accumulator = fn(accumulator, list[index + 1]);
result = foldl(fn)(list, accumulator, index + 1);
}
return result;
};
}