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

add mapValues to Map<K, V> #9590

Closed
polux opened this issue Apr 2, 2013 · 8 comments
Closed

add mapValues to Map<K, V> #9590

polux opened this issue Apr 2, 2013 · 8 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-2 library-collection P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@polux
Copy link
Contributor

polux commented Apr 2, 2013

I often find myself writing

Map newMap = new Map();
m.forEach((k, v) {
  newMap[k] = f(v);
});
...

It would be great if Map had a map or mapValues method instead:

Map newMap = m.mapValues(f);

@DartBot
Copy link

DartBot commented Apr 2, 2013

This comment was originally written by @seaneagan


If we had issue #7088, then could do:

var newMap = new Map.fromPairs<K, V>(m.pairs.map((pair) => new Pair(pair.key, f(pair.value))));

and if Map instead directly extended Iterable<Pair<K, V>> it could be:

var newMap = new Map.from(m.map((pair) => new Pair(pair.key, f(pair.value))));

@DartBot
Copy link

DartBot commented Apr 8, 2013

This comment was originally written by [email protected]


Added Area-Library label.

@anders-sandholm
Copy link
Contributor

Added Triaged label.

@DartBot
Copy link

DartBot commented May 5, 2013

This comment was originally written by @seaneagan


FWIW I've been using this in the meantime:

Map mapValues(Map map, var valueMapper(key)) => map.keys.fold({}, (result, key) {
  result[key] = valueMapper(map[key]);
  return result;
});

@DartBot
Copy link

DartBot commented Jan 23, 2014

This comment was originally written by @seaneagan


An alternative would be to add optional key and value callbacks to Map.from similar to Map.fromIterable:

factory Map.from(Map other, {K key(key), V value(value)});

@kasperl
Copy link

kasperl commented Dec 4, 2014

Issue #21780 has been merged into this issue.

@lrhn
Copy link
Member

lrhn commented Dec 5, 2014

You can use Map.fromIterables:

  var mappedMap = new Map.fromIterables(map.keys, map.values.map(transform));

There is a limit to how many specialized transformers we want to add.

Another alternative is to create a wrapper. It's not that complex:

  import "dart:collection";
  class MapMap<K,V> extends UnmodifiableMapBase<K,V> {
    final Map<K, dynamic> _map;
    final Function _valueTransform; // Should map null to null.
    MapMap(Map<K, dynamic> map, V valueTransform(oldValue))
         : _map = map, _valueTransform = valueTransform;
    V operator[](K key) => _valueTransform(_map[key]);
    Iterable<K> get keys => _map.keys;
  }
    

@polux polux added Type-Enhancement library-collection area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Dec 5, 2014
@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed triaged labels Feb 29, 2016
@lrhn lrhn added the core-m label Aug 11, 2017
@floitschG floitschG added core-2 and removed core-m labels Aug 29, 2017
@srawlins
Copy link
Member

Map.map has landed.

@bkonyi bkonyi closed this as completed Jun 18, 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-2 library-collection P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

9 participants