Hacker News new | ask | show | jobs
by coroutines 3710 days ago
What really pisses me off are the posts about Coffeescript being supplanted by ES6 like it's a complete substitute. Array comprehensions are just one example of what JS folks are still missing out on.
1 comments

List comprehensions are incredible. But they're just syntactic sugar for map() and filter() and lambdas.

Python's

  prev = [1,2,3,4]
  next = [x * 2 for x in old]
Is the same as javascript's

  prev = [1,2,3,4];
  next = prev.map(x => x * 2);
Python's

  next = [x for x in prev if x % 2 == 0]
is the same as javascript's

  next = prev.filter(x => x % 2 === 0);
And finally, python's

  next = [x * 2 for x in prev if x % 2 == 0]
is the same as

  next = prev.filter(x => x % 2 == 0).map(x => x * 2);
It could be nice to have a "mapIf" function that accepts a mapping and a predicate, but I think the extra verbosity makes the above code easier to understand.

  next = prev.mapIf(x => x * 2, x => x % 2 == 0);
Thanks for taking the time to type this out.

At this point it feels to me, and probably to many other devs and also JS engine implementors, that list comprehensions are such a small improvement over map() and filter() that for now it may simply not be worth introducing another feature into the language when there's already so much being worked on, especially things that will yield much more pronounced improvements to JS language ergonomics with not that much more effort required than for example list comprehensions would take.

(Maybe if we tackle the addition of list comprehensions at a later point, we'll be able to get that neat-o postfix if statement in as well, for a low cost. But I'm just daydreaming (mostly) and speculating (a bit) here!)

(Edit: formatting)

I think it's important to remember that Array::map() and Array::filter() construct new Arrays (even if the old one is quickly "lost" and collected). It's probable v8 sees this and does some trickery to make use of the existing allocated Array, but I doubt it.

let next = [ 1, 2, 3, 4 ].map(x => x * 2);

versus:

next = (x * 2 for x in [ 1 .. 4 ])

...which becomes:

// Generated by CoffeeScript 1.10.0

(function() { var next, x;

  next = (function() {
    var i, results;
    results = [];
    for (x = i = 1; i <= 4; x = ++i) {
      results.push(x * 2);
    }
    return results;
  })();
}).call(this);

Array::map()/filter() get points for encouraging immutable transformations.

This could probably be solved if Javascript had map/filter/reduce functions for iterators (and hopefully dictionaries) as well as arrays.

And of course a range(start,stop,step) iterator similar to python's range.

it would then be a matter of:

  next = range(1,40000).map(x => x * 2);
and because range would be an iterator, the only array created would be next.
Everyone seems to forget nested comprehensions

  [x + y for x in [1,4,9] for y in [2,4,6] if x < y]