Skip to main content
Module

x/ramda/source/mapAccumRight.js

:ram: Practical functional Javascript
Latest
File
import _curry3 from './internal/_curry3.js';

/** * The `mapAccumRight` function behaves like a combination of map and reduce; it * applies a function to each element of a list, passing an accumulating * parameter from right to left, and returning a final value of this * accumulator together with the new list. * * Similar to [`mapAccum`](#mapAccum), except moves through the input list from * the right to the left. * * The iterator function receives two arguments, *acc* and *value*, and should * return a tuple *[acc, value]*. * * @func * @memberOf R * @since v0.10.0 * @category List * @sig ((acc, x) -> (acc, y)) -> acc -> [x] -> (acc, [y]) * @param {Function} fn The function to be called on every element of the input `list`. * @param {*} acc The accumulator value. * @param {Array} list The list to iterate over. * @return {*} The final, accumulated value. * @see R.addIndex, R.mapAccum * @example * * const digits = ['1', '2', '3', '4']; * const appender = (a, b) => [b + a, b + a]; * * R.mapAccumRight(appender, 5, digits); //=> ['12345', ['12345', '2345', '345', '45']] * @symb R.mapAccumRight(f, a, [b, c, d]) = [ * f(f(f(a, d)[0], c)[0], b)[0], * [ * f(a, d)[1], * f(f(a, d)[0], c)[1], * f(f(f(a, d)[0], c)[0], b)[1] * ] * ] */var mapAccumRight = _curry3(function mapAccumRight(fn, acc, list) { var idx = list.length - 1; var result = []; var tuple = [acc]; while (idx >= 0) { tuple = fn(tuple[0], list[idx]); result[idx] = tuple[1]; idx -= 1; } return [tuple[0], result];});export default mapAccumRight;