`.toArray()` for all generators

Claude Pache claude.pache at gmail.com
Wed Aug 28 09:40:13 PDT 2013


Note that an equivalent of both `.map` and `.filter` already exists in ES6: its name is "generator expression". For instance:

```
	array.filter(pred).map(transf)
```
becomes:
```
	(for (let x of iter) if (pred(x)) transf(x))
```

—Claude


Le 28 août 2013 à 17:27, Forbes Lindesay <forbes at lindesay.co.uk> a écrit :

> The thing about `.map` and `.filter` is that it would be easy enough to create functional versions:
> 
> ```js
> function* map(array, fn) {
>  for (var x of array) {
>    yield fn(x)
>  }
> }
> ```
> 
> But harder to create methods:
> 
> ```js
> Iteratable.prototype.map = function(fn) {
>  for (var x of this) {
>    yield fn(x)
>  }
> }
> ```
> 
> The reason being that not all iteratables share a prototype (to my knowledge). 
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss



More information about the es-discuss mailing list