source: node_modules/ramda/es/mapAccum.js

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

Initial commit

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