Pages

2013-01-09

ECMAScript.next: array comprehensions and generator comprehensions

Update 2014-08-04: Comprehensions won’t be in ECMAScript 6. There’s a chance that an updated version of them will be in ECMAScript 7 or later.

ECMAScript.next will have two kinds of comprehensions: array comprehensions and generator comprehensions. They allow one to quickly assemble an array or a sequence of elements. Comprehensions exist in many programming languages, for example: CoffeeScript, Python, Haskell, Clojure.

Array comprehensions

Array comprehensions look like this:

[for (x of a) for (y of b) if (x > y) [x,y]]

This is equivalent to calling the following function (which uses ECMAScript.next’s for-of loop [1]).

There can be two kinds of clauses inside an array comprehension, in any order (but the first clause must be a for):

for

if

Array comprehensions are especially convenient for translating one array to another one:

let numbers = [1,2,3];
let squares = [for (x of numbers) x*x];

However, Array.prototype.map becomes more convenient in ECMAScript.next, too, because there will be arrow functions [2]:

let squares = numbers.map(x => x * x);

Generator comprehensions

Generator comprehensions work and look almost like array comprehensions. But they are delimited by parentheses and produce a generator object [1]. That object yields the elements instead of putting them in an array (which means that you can work with potentially infinite sequences). For example: