4 ms·
Generator expressions: [ y for ( y in [5,6,7,8,9] ) ] // is [0,1,2,3,4] and [ y for each ( y in [5,6,7,8,9] ) ] // is [5,6,7,8,9] Because in for ext
by jauco 17y ago
Generator expressions:
[ y for ( y in [5,6,7,8,9] ) ] // is [0,1,2,3,4]
and
[ y for each ( y in [5,6,7,8,9] ) ] // is [5,6,7,8,9]
Because in for extracts index names, and for each extracts the values.
- tumult 17y agoOh dude, you're the man. I didn't even know you could do comprehensions in JS.
- tlrobinson 17y agoOf course it's not cross-browser, but if you're doing server-side JS it might be useful.
- mpk 17y agoIt's not just server-side or client side these days. Using Rhino I'm using JS for compressing JS files for production and generating documentation for said JS files.
- jamongkad 17y agoYou can do list comprehensions in JS now??
- jeroen 17y agoSince 1.8: http://en.wikipedia.org/wiki/List_comprehension#Javascript_1.8 http://en.wikipedia.org/wiki/List_comprehension#Javascript_1... It will probably be a long time before 1.8 is supported by all browsers.
- boucher 17y agoit will probably be never. unless all browsers ceases to include internet explorer at some point.
- zackattack 17y agoWhat are generator expressions?
- jauco 17y agoA generator expression is an easy way the create a function that performs a small part of the calculation each time it's run (and has memory of the state it's in). Since code often uses a one item at a time, this is less memory intensive then first generating the complete list and storing it in memory. http://www.python.org/dev/peps/pep-0289/ http://www.python.org/dev/peps/pep-0289/ The generator part in the example is the '(y in [])' the rest is a list comprehension (a short way of writing for loops that can be implemented very efficiently in a language and will return a list). [UPDATE] Found a better explanation that also explains expression closures at http://ejohn.org/blog/javascript-18-progress/ http://ejohn.org/blog/javascript-18-progress/