Table of Contents

**This proposal has progressed to the Draft ECMAScript 6 Specification, which is available for review here: specification_drafts. Any new issues relating to them should be filed as bugs at http://bugs.ecmascript.org. The content on this page is for historic record only and may no longer reflect the current state of the feature described within.**

Generator expressions were introduced in JavaScript 1.8. Generator expressions are a convenient, declarative form for creating generators with a syntax based on array comprehensions. Generator expressions also provide a convenient refactoring pattern, making it easy to switch between eager and on-demand generation of values in a sequence simply by changing the bracketing.

Because generator expressions desugar to an application of iterators, the surface syntax uses `for`

-`of`

rather than `for`

-`in`

.

Extracting pages on demand from an array of URL’s:

(xhrGet(url) for (url of getURLs()))

Filtering a sequence:

(x for (x of generateValues()) if (x.color === ‘blue’))

Lazy cartesian product

(xhrGet(row, column) for (row of rows()) for (column of columns()))

PrimaryExpression ::= ... | "(" Expression ("for" "(" LHSExpression "of" Expression")")+ ("if" "(" Expression ")")? ")"

A generator expression:

**(** *Expression _{0}*

can be defined by expansion to the expression:

**(function () {**

**for (let ***LHSExpression _{1}*

...

Background motivation for the syntactic sugar afforded by generator expressions:

- Peter Norvig’s Sudoku solver based on constraint propagation, written in Python

The critical uses of generator expressions, e.g., the actual parameter to `all`

in:

if (all(eliminate(values, s, d2) for (d2 of values[s]) if (d2 != d))) return values;

can only be desugared to generation function applications or an equivalent lazy iterator construct. They cannot be replaced with array comprehensions or any such eager construct without the solver taking exponential time and space creating eagerly populated arrays where it would have stopped early using lazy generator expressions, thanks to constraint propagation. Note how `all`

is defined:

function all(seq) { for (let e in seq) if (!e) return false; return true; }

so as to stop as soon as a value in the iterated sequence is falsy.

The JS1.8 version has some XXX comments and helper functions that show where methods such as the Array extras (`Array.prototype.every`

instead of the custom `all`

shown above, e.g.) are not iterator-friendly. This suggests the need for more generic methods that abstract over arrays and iterators.

— *Brendan Eich 2010/06/27 19:47*

Another note: `yield`

is verboten in a generator expression, to uphold TCP.

— *Brendan Eich 2011/03/24 18:12*