Echo JS 0.11.0

<~>
CrossEye 3635 days ago. link parent 3 points
In more detail, this would let you decide to build up functions which then could be reused whenever you need them, deciding only at the end whether to pass the data to them to get the results, or skip it to get a new function, which can later be passed the data.

A simpler example might be this:

    // Common setup for both examples

    var add = function(x, y) {return x + y;}
    var numbers = [1, 2, 3, 4, 5];

    // LoDash/Underscore
    // =================
    // If you want to total up the first five numbers, you can do:

    var sum = _.reduce(numbers, add, 0); //=> 15

    // But if you want a reusable function that can total various different lists:

    var total = function(list) {
        return _.reduce(list, add, 0);
    };
    var sum = total(numbers) //=> 15


    // Ramda
    // =====
    // If you want to total up the first five numbers, you can do:

    var sum = R.reduce(add, 0, numbers; //=> 15

    // But if you want a reusable function that can total various different lists:

    var total = R.reduce(add, 0);
    var sum = total(numbers) //=> 15


Notice that in Ramda, the version that creates the function is almost the same as the version that computes the result; you simply leave off the final parameter -- which then becomes a parameter to the new function.  That's because ramda's `reduce` is automatically curried.

Ramda's main contribution is to make it easy to build such functions.  By taking the data parameters last and automatically currying the built-in functions, Ramda makes it very easy for you to compose your data manipulations functions out of simple pieces.

DISCLAIMER:  I am a co-author of Ramda, and therefore biased in it's favor, although I don't think I have anything against Underscore or LoDash, which I've used to great effect in numerous projects.

Replies