Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Map ctor: Map.convert (or optional args to .from) #11751

Closed
kevmoo opened this issue Jul 9, 2013 · 9 comments
Closed

New Map ctor: Map.convert (or optional args to .from) #11751

kevmoo opened this issue Jul 9, 2013 · 9 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-n library-core type-enhancement A request for a change that isn't a bug

Comments

@kevmoo
Copy link
Member

kevmoo commented Jul 9, 2013

See https://code.google.com/p/dart/issues/detail?id=11750

Map.convert(Map other, {K keySelector(key, value), V valueSelector(key, value)})

The default keySelector selects the keys. The default valueSelector selects the values.
So the default behavior is the same as Map.from(map)

I might like this more than the suggestion in 11750, if it was adopted by the other Map subclasses. Then one gets the flexibility of choosing which subclass to use with the same functionality.

@iposva-google
Copy link
Contributor

Added Area-Library, Triaged labels.

@lrhn
Copy link
Member

lrhn commented Aug 22, 2013

We could make keySelector and valueSelector optional arguments on Map.from.


Removed Type-Defect label.
Added Type-Enhancement label.

@kevmoo
Copy link
Member Author

kevmoo commented Aug 30, 2013

That's good, too.

@floitschG
Copy link
Contributor

Why not just use new Map.fromIterables(map.keys.map(keySelector), map.values.map(valueSelector)) or
Map.fromIterable(map.keys, (key) => valueSelector(map[key])); ?

@kevmoo kevmoo added Type-Enhancement library-core area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Sep 2, 2013
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed priority-unassigned labels Feb 29, 2016
@kevmoo kevmoo changed the title New Map ctor: Map.convert New Map ctor: Map.convert (or optional args to .from) Jun 22, 2017
@kevmoo
Copy link
Member Author

kevmoo commented Jun 22, 2017

Bumping this again.

.fromIterables requires 2 iterations through the source map. Depending on the implementation, this could be pretty expensive.

@lrhn
Copy link
Member

lrhn commented Jun 22, 2017

A pairs/entries iterable on Map looks like it could be the most general solution.
It will cost an extra allocation per entry, but it will most likely be short-lived.

@lrhn lrhn added core-m and removed core-m labels Aug 11, 2017
@floitschG
Copy link
Contributor

Also see #9590 which asks for the same thing as instance-method.

@rakudrama
Copy link
Member

I think there are nicer solutions:

  • A MapIterator that allows access to currentKey and currentValue without allocating Entry objects. Perhaps this could subclass Iterator (i.e. with accessors current and currentValue).
  • Rank 2 iterables is a previous attempt to advocate for something more usable in this space.
  • Extending for-in with syntax to bind keys and values. I have seen several cases of awkward code using Map.forEach on scanning loops that should have an early exit or return.

@floitschG floitschG removed the core-m label Aug 28, 2017
@kevmoo
Copy link
Member Author

kevmoo commented Jun 27, 2018

I'm super happy with fromEntries now...

@kevmoo kevmoo closed this as completed Jun 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-n library-core type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants