union method

Map<K, V> union(
  1. Eq<K> eq,
  2. V combine(
    1. V x,
    2. V y
    ),
  3. Map<K, V> map
)

Combine the key/value of this Map and map using combine where the key is the same.

Implementation

Map<K, V> union(
  Eq<K> eq,
  V Function(V x, V y) combine,
  Map<K, V> map,
) {
  var result = {...this};
  for (var entry in map.entries) {
    if (lookupKeyEq(eq, entry.key) case Some(value: var key)) {
      result.update(key, (v) => combine(entry.value, v));
    } else {
      result[entry.key] = entry.value;
    }
  }
  return result;
}