modifyAt method

Option<Map<K, V>> modifyAt(
  1. Eq<K> eq,
  2. V update(
    1. V value
    ),
  3. K key
)

If the given key is present in the Map, then modify its value using update and return the Map.

If multiple keys equal to key exist in the map, all of them are updated.

Otherwise, return None.

Implementation

Option<Map<K, V>> modifyAt(
  Eq<K> eq,
  V Function(V value) update,
  K key,
) {
  for (var entryKey in keys) {
    if (eq.eqv(entryKey, key)) {
      // At least one equal key exists in map.
      return some({
        for (var entry in entries)
          entry.key:
              eq.eqv(entry.key, key) ? update(entry.value) : entry.value
      });
    }
  }
  return const None();
}