mergeMaps<K, V> function

Map<K, V> mergeMaps<K, V>(
  1. Map<K, V> a,
  2. Map<K, V> b,
  3. V conflict(
    1. K,
    2. V,
    3. V
    )
)

Merge Two Maps with Conflict Resolution. If a key exists in both, conflict is called with (key, aValue, bValue).

Implementation

Map<K, V> mergeMaps<K, V>(
  Map<K, V> a,
  Map<K, V> b,
  V Function(K, V, V) conflict,
) {
  final result = Map<K, V>.from(a);
  b.forEach((k, v) {
    if (result.containsKey(k)) {
      result[k] = conflict(k, result[k] as V, v);
    } else {
      result[k] = v;
    }
  });
  return result;
}