Map: filter/map and more

Mark Volkmann r.mark.volkmann at gmail.com
Thu Nov 20 10:36:04 PST 2014


Where can I read about the rationale to not put those methods on the Map
prototype? I'm curious why that was okay for the Array class, but not okay
for Map and Set.

On Thu, Nov 20, 2014 at 12:32 PM, Axel Rauschmayer <axel at rauschma.de> wrote:

> 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
>
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>


-- 
R. Mark Volkmann
Object Computing, Inc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141120/d9ed90a8/attachment.html>


More information about the es-discuss mailing list