ES7 Array and Generator comprehensions

added by bpwndaddy
6/9/2015 3:29:35 PM

3 Kicks, 165 Views

Array comprehension is a new feature proposed for ES7, with a new syntax to create new arrays from existing iterables, comprehensions can replace map and filter. Generator comprehension brings the same feature to generators, this is a more useful feature as it removes the need to write new generators for simple map/filter operations. Generator comprehensions allow us to easily write single-line generators, which can replace our arrays in some situations, you might ask why we might consider replacing arrays with generators, the most important reason is their laziness. I’ve explained laziness later in the article. Comprehensions are currently only supported by Firefox, use Firefox 30+ or Babel to run the examples. The Node.js version of examples using generator function* ()s is available at the repository (doesn’t require transpilation, use latest node).