Skip to content

Comment on Intro to CoffeeScriptparent

Comments

    var newArray = _.map(_.filter(array, function(item) {
        return item % 2;
    }), function(item) {
        return item + 1;
    });
vs
    newArray = (item + 1 for item in array when item % 2)
Uh huh.

You could also do it with chain, which could make the first example a bit simpler to read and write. It is something like:

    var newArray = _.chain(array).filter(function(item) {
            return item % 2;
        }).map(function(item) {
            return item + 1;
        }).value();
I often use CoffeeScript + Underscore

You don't even need to call _.chain or value – underscore already does this automatically. You can also write it like so, if you like:

  var newArray = _(array).filter(function(item) {
        return item % 2;
    }).map(function(item) {
        return item + 1;
    });
Personally, I find this quite readable. And honestly, my comment was more about comparing CoffeeScript to its compiled JavaScript output, as is done in the slides. The compiled output is incredibly verbose and gives the false impression that JavaScript is always complicated.
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.