mapMap<K1, V1, K2, V2> function

Map<K2, V2> mapMap <K1, V1, K2, V2>(
  1. Map<K1, V1> map,
  2. {K2 key(
    1. K1 key,
    2. V1 value
    ),
  3. V2 value(
    1. K1 key,
    2. 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;
}