Hi!

I have a Map and I want to replace the value associated to a key with 
something that depends on that value, so that can only work if that value 
does exist in the Map. For example:

~~~
map = %{1 => 2}
key = 1
Map.replace(map, key, map[key] + 2) # %{1 => 4}
~~~

Now, `key` is not known to me but I'm sure that if it's present in the 
`map` I will be able to perform the operation on the value (so `map[key]` 
will always be a number, in this example). If `key` is not present then I 
will get an error:

~~~
map = %{1 => 2}
key = 3
Map.replace(map, key, map[key] + 2) # (ArithmeticError)
~~~

So I'm thinking maybe there should be Map.replace_lazy (there's already 
Map.get_lazy, Map.pop_lazy and Map.put_new_lazy). I also checked other Map 
functions that have a `fun` as a last argument and none allow to me to the 
above. There's `Map.update(map, key, initial, fun)` but I don't want an 
initial value, I just want to update the value if the key exists.

Another alternative it to let Map.replace be a macro so that the value can 
be unquoted but only if the key is present.

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/e0006ca3-660c-433f-859b-430e1ce8cfbb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to