forbes at lindesay.co.uk (2016-02-01T12:59:54.312Z)
> ```
> ({a => 10}).mapWithKey((k, v) => ['b', 20]); // {b => 20} or {a => 20, b
> => 20} ?
> ```
Whoops, nevermind actually, since we don't mutate the original map (the
`this`), but rather return a new one, it should always be just `{b => 20}`,
and `a` key is ignored in the transformed map.
Dmitry
On Sat, Oct 4, 2014 at 10:03 PM, Dmitry Soshnikov < dmitry.soshnikov at gmail.com> wrote: > On Sat, Oct 4, 2014 at 5:58 PM, Domenic Denicola < > domenic at domenicdenicola.com> wrote: > >> From: es-discuss [mailto:es-discuss-bounces at 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} ? > ``` > Whoops, nevermind actually, since we don't mutate the original map (the `this`), but rather return a new one, it should always be just `{b => 20}`, and `a` key is ignored in the transformed map. Dmitry -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141004/1b7e4abd/attachment-0001.html>