How to use the mapAccum function from ramda

Find comprehensive JavaScript ramda.mapAccum code examples handpicked from public code repositorys.

ramda.mapAccum is a function that maps over a list, passing an accumulating parameter and the current element to a provided function, and returns an array containing the final accumulated value and a new list with the results of applying the function to each element.

18
19
20
21
22
23
24
25
26
27
28
    const all = R.map(async need => await need(), init);
    const allEntities = await Promise.all(all);
    cache = cache.concat(allEntities);
    return R.compose(
        R.head,
        R.mapAccum((acc, data) => [Object.assign(acc, R.head(data)), data], {})
    )(allEntities);
};


const build = function (entity, useValue, name) {
fork icon4
star icon0
watch icon0

2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
 * @example
 *
 *      var digits = ['1', '2', '3', '4'];
 *      var appender = (a, b) => [a + b, a + b];
 *
 *      R.mapAccum(appender, 0, digits); //=> ['01234', ['01', '012', '0123', '01234']]
 */
var mapAccum = _curry3(function mapAccum(fn, acc, list) {
    var idx = 0;
    var len = list.length;
fork icon0
star icon0
watch icon0

+ 17 other calls in file

How does ramda.mapAccum work?

ramda.mapAccum is a function in the Ramda library that applies a function to each element of a list, accumulating the results while returning a tuple containing the accumulated value and a new list.

9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
*
*      const digits = ['1', '2', '3', '4'];
*      const appender = (a, b) => [a + b, a + b];
*
*      R.mapAccum(appender, 0, digits); //=> ['01234', ['01', '012', '0123', '01234']]
* @symb R.mapAccum(f, a, [b, c, d]) = [
*   f(f(f(a, b)[0], c)[0], d)[0],
*   [
*     f(a, b)[1],
*     f(f(a, b)[0], c)[1],
fork icon0
star icon0
watch icon2

+ 7 other calls in file

Ai Example

1
2
3
4
5
const R = require("ramda");

const addAndDouble = (acc, x) => [acc + x, 2 * x];
const result = R.mapAccum(addAndDouble, 0, [1, 2, 3, 4]);
console.log(result); // [10, [2, 4, 6, 8]]

In this example, mapAccum applies the addAndDouble function to each element of the array [1, 2, 3, 4] and an accumulator 0. The addAndDouble function takes an accumulator and an array element, adds the accumulator and the array element, and returns a tuple with the new accumulator and the doubled array element. The mapAccum function returns a tuple with the final accumulator value and an array with the doubled array elements. In this case, the final accumulator value is 10 and the array with the doubled array elements is [2, 4, 6, 8].

Other functions in ramda

Sorted by popularity

function icon

ramda.clone is the most popular function in ramda (30311 examples)