source: node_modules/ramda/es/mapAccumRight.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.6 KB
Line 
1import _curry3 from "./internal/_curry3.js";
2/**
3 * The `mapAccumRight` function behaves like a combination of map and reduce; it
4 * applies a function to each element of a list, passing an accumulating
5 * parameter from right to left, and returning a final value of this
6 * accumulator together with the new list.
7 *
8 * Similar to [`mapAccum`](#mapAccum), except moves through the input list from
9 * the right to the left.
10 *
11 * The iterator function receives two arguments, *acc* and *value*, and should
12 * return a tuple *[acc, value]*.
13 *
14 * @func
15 * @memberOf R
16 * @since v0.10.0
17 * @category List
18 * @sig ((acc, x) -> (acc, y)) -> acc -> [x] -> (acc, [y])
19 * @param {Function} fn The function to be called on every element of the input `list`.
20 * @param {*} acc The accumulator value.
21 * @param {Array} list The list to iterate over.
22 * @return {*} The final, accumulated value.
23 * @see R.addIndex, R.mapAccum
24 * @example
25 *
26 * const digits = ['1', '2', '3', '4'];
27 * const appender = (a, b) => [b + a, b + a];
28 *
29 * R.mapAccumRight(appender, 5, digits); //=> ['12345', ['12345', '2345', '345', '45']]
30 * @symb R.mapAccumRight(f, a, [b, c, d]) = [
31 * f(f(f(a, d)[0], c)[0], b)[0],
32 * [
33 * f(a, d)[1],
34 * f(f(a, d)[0], c)[1],
35 * f(f(f(a, d)[0], c)[0], b)[1]
36 * ]
37 * ]
38 */
39
40var mapAccumRight =
41/*#__PURE__*/
42_curry3(function mapAccumRight(fn, acc, list) {
43 var idx = list.length - 1;
44 var result = [];
45 var tuple = [acc];
46
47 while (idx >= 0) {
48 tuple = fn(tuple[0], list[idx]);
49 result[idx] = tuple[1];
50 idx -= 1;
51 }
52
53 return [tuple[0], result];
54});
55
56export default mapAccumRight;
Note: See TracBrowser for help on using the repository browser.