In the last few days, I did some formal reading and informal experimenting with EcmaScript6, and compiled the list of its most annoying “features”.
The fifth place goes to complete lack of standard functions for iterables. EcmaScript 6 introduces a lot of cool stuff, but it brings a fair share of new WTFs, and adds new thrill to some existing ones. ES6 defines the concept of iterable and the for..of
loop. You can now write something like this:
function *range(start, len) {
for (var i=start; i<start+len; ++i) yield i;
}
for (var n of range(3,4)) console.log(n);
Most languages that define iterables (a.k.a enumerables, sequences, comprehensions) come with standard methods that operate on them, such as map()
, filter()
, or groupBy()
, but JavaScript has none. It does have map()
, filter()
, reduce()
, but only for arrays:
var arr = Array.from(range(1,10));
console.log(arr);
console.log(arr.map(n=>n*n));
console.log(...range(1,10));
console.log(...range(1,10).map(n=>n*n));
Of course, one can define necessary functions with relative ease, but their omission from the standard is very annoying. Most articles on iterables simply ignore the subject. I found a library called wu.js that claims to implement them, but their ES6 download link is broken. There is also a library called functify. I also found comments from several people that said they tried to write a similar library and it was fun, but the result was horribly inefficient. If this is indeed the case, it makes iterables virtually useless.
CodeProject