ShakaCode | ShakaCode Blog | Rails On Maui Blog | Rails | ReactJs | JavaScript | Webpack | Productivity |

es7 Array and Generator Comprehensions


#1

Babel Doc

The Babel doc is pretty vague:
http://babeljs.io/docs/advanced/transformers/other/comprehensions/

The MDN doc is better

The key difference is that the generator comprehension yields a multiple values, which can be put into an array with the spread operator.

Array Comprehensions

[for (i of [ 1, 2, 3 ]) i*i ]; 
// [ 1, 4, 9 ]

var abc = [ "A", "B", "C" ];
[for (letters of abc) letters.toLowerCase()];
// [ "a", "b", "c" ]

Generator Comprehensions

(for (i of [ 1, 2, 3 ]) i*i );
// generator function which yields 1, 4, and 9

[...(for (i of [ 1, 2, 3 ]) i*i )];
// [1, 4, 9]

var abc = [ "A", "B", "C" ];
(for (letters of abc) letters.toLowerCase());
// generator function which yields "a", "b", and "c"
```

# Questions
1. Does anybody know of any full ES7 fiddles? I tried http://codepen.io/justin808/pen/GJVoaJ?editors=001 but I got a syntax error.
2. What's the general use case of where you want multiple values, rather than an Array? Is Array usage more common?