mapMap<K1, V1, K2, V2> function

Map<K2, V2> mapMap <K1, V1, K2, V2>(Map<K1, V1> map, { K2 key(K1 key, V1 value), V2 value(K1 key, V1 value) })

Creates a new map from map with new keys and values.

The return values of key are used as the keys and the return values of value are used as the values for the new map.

Implementation

Map<K2, V2> mapMap<K1, V1, K2, V2>(Map<K1, V1> map,
    {K2 key(K1 key, V1 value), V2 value(K1 key, V1 value)}) {
  key ??= (mapKey, _) => mapKey as K2;
  value ??= (_, mapValue) => mapValue as V2;

  var result = <K2, V2>{};
  map.forEach((mapKey, mapValue) {
    result[key(mapKey, mapValue)] = value(mapKey, mapValue);
  });
  return result;
}