On Sat, Oct 4, 2014 at 10:03 PM, Dmitry Soshnikov < dmitry.soshni...@gmail.com> wrote:
> On Sat, Oct 4, 2014 at 5:58 PM, Domenic Denicola < > dome...@domenicdenicola.com> wrote: > >> From: es-discuss [mailto:es-discuss-boun...@mozilla.org] On Behalf Of >> Domenic Denicola >> >> > Unless I'm missing something (quite possible!), I would prefer not to >> add new methods to Map and Set when they could be added to >> %IteratorPrototype%. >> >> Yeah, I'm missing something. It's the difference in code between: >> >> ```js >> var newMap = oldMap.map(([k, v]) => [k + 1, v + 1]); >> ``` >> >> versus >> >> ```js >> var newMap = new Map(oldMap.entries().map((([k, v]) => [k + 1, v + 1]); >> ``` >> >> I think I still prefer avoiding every iterable subclass adding its own >> map/filter/etc. in favor of people using the compositional base primitives, >> but at least I see the argument now. >> > > We already have `Map#forEach`, and correlation with `Array#forEach -> > `Array#map` makes this constancy intuitive. > > The only thing which Rick mentioned, is that `Map#map` should probably > return a transformed `[key, value]` pair, that allows updating the key as > well. However, need to think about it, since as mentioned on the diff [1], > usually people transform only values, and to force them always return an > array may be annoying (the key is not transformed in this case), and less > performant in terms useless array allocation. > > This case I'd probably make as `Map#mapWithKey`, however there other > questions arise like, should the returned pair replace the existing, or > create a new one, if the key is different? > > ``` > ({a => 10}).mapWithKey((k, v) => ['b', 20]); // {b => 20} or {a => 20, b > => 20} ? > ``` > > But this is for further discussion. > > I think it's definitely worth discussing the generic collection prototype, however at the meantime, an updated version of the mapping function, here: https://gist.github.com/DmitrySoshnikov/a218700746b2d7a7d2c8 - Moved the mapping to the abstract `MapTransform` - Reused it for `Map#map` and `Map#mapEntries` (which updates the keys as well) Dmitry
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss