2006-02-21
Javascript and Generators
Could Javascript be getting generators, iterators and list comprehensions? This post from Brendan Eich is a status update about the future of Javascript that mentions the possibility (From Ajaxian).
js> function count(n) {
for (var i = 0; i < n; i++)
yield i;
}
js> g = count(10)
[object Generator]
js> g.next()
0
js> g.next()
1
js> two_to_nine = [i for i in g]
2,3,4,5,6,7,8,9
js> squares_to_20 = [i * i for i in count(20)]
0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361