javascript ramda.js

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript ramda.js相关的知识,希望对你有一定的参考价值。

/*
 * Using the library ramda, what is the result of the following code?
 * R.reduce((acc,x) => R.compose(R.flip(R.prepend)(acc), R.sum,R.map(R.add(1)))([x,...acc]), [0])([13, 28]);
 * Explain each of the steps the best you can.
 */

const R = require('ramda');
// Reduce function iterating over [13, 28]
// starting point: [0]
const test = R.reduce(
    (acc, x) => {
        console.log(acc, x);
        // Compose - right to left function taking in [...acc, x]
        // (read everyting within compose from bottom to top)
        return R.compose(
            // 4. Prepend sum to accumulator array
            R.flip(R.prepend)(acc),
            // 3. sum of array
            R.sum,
            // 2. 1++ for each array item
            R.map(R.add(1)),
            // 1. Take in array of x and acc values
        )([...acc, x]);
    },
    [0],
)([13, 28]);

console.log(test);

// First iteration: accumulator is [0].
// Compose function takes in [0, 13]
// 1++ for each item in array:  [14, 1]
// Sum is 15
// Prepend sum to accumulator array: [15, 0]

// Second iteration: accumulator is [15, 0]
// compose function takes in [0, 15, 28]
// 1++ for each item in array: 1, 16, 29
// Sum of [1, 16, 29] is 46
// Prepend sum to accumulator: [46, 15, 0]

// Does `flip` even flip anything?

以上是关于javascript ramda.js的主要内容,如果未能解决你的问题,请参考以下文章

javascript ramda.js

Ramda js:具有嵌套对象数组的深度嵌套对象的镜头

[Transducer + Ramda] Write highly performance / functional code by using transducer-js and ramda.js

ramda js 在第二和第三级更新深度嵌套的数组

JavaScript介绍

javascript的题。