Map: filter/map and more
Axel Rauschmayer
axel at rauschma.de
Thu Nov 20 10:32:57 PST 2014
> At the meeting it was decided not to go with `map` and `filter` sitting on `Map.prototype`, but instead to use iterators in the way like:
>
> ```
> map
> .entries() // returns an iterator
> .map((v, k, m) => { ... })
> .filter((v, k, m) => { ... })
> .collect(); // returns a new map after all transforms
> ```
Convenient, but this pattern couldn’t be extended to new Map classes or other collections. I see two alternatives.
First, use the `Map` constructor. I don’t find this too bad and it’s self-explanatory.
```js
new Map(map
.entries() // returns an iterator
.map((v, k, m) => { ... })
.filter((v, k, m) => { ... }));
```
Second:
```js
map
.entries() // returns an iterator
.map((v, k, m) => { ... })
.filter((v, k, m) => { ... })
.collectInto(new Map());
```
`collectInto(coll)` invokes `coll.set(k,v)` for each pair `[k,v]` in the sequence. It returns `coll`.
--
Dr. Axel Rauschmayer
axel at rauschma.de
rauschma.de
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141120/2dbf817b/attachment.html>
More information about the es-discuss
mailing list