I really like this and was thinking about this just yesterday, but was 
struggling to come up with a name. I was thinking of "update_if_existing" but 
"replace_with" is actually fantastic, follows our conventions, and sounds 
great. Awesome job!! If others on the core team agree, I think you could send 
out a PR for this 😊

> On 11 Dec 2021, at 22:24, Mackenzie Morgan <maco...@gmail.com> wrote:
> 
> Today's Advent of Code puzzle had a handful of us in the Elixir slack 
> wishing for something that bridged the gap between Map.update/4, 
> Map.update!/3, and Map.replace/3. 
> 
> Map.update/4 litters your map with extra keys you then have to find and 
> filter out.
> Map.update!/3 throws exceptions which can be especially undesirable in a 
> pipeline.
> Map.replace/3 requires you to pre-calculate the new value.
> 
> The goal would be "if this key exists in the map, update its value using this 
> function." 
> 
> I wrote myself a little helper function (below) to wrap Map.replace/3, and I 
> know someone else said they made a bunch of {key, nil} s and then had to 
> filter them back out.
> 
> Anyway, Advent of Code probably isn't the only time it'd be helpful to have 
> that syntactic sugar. Thoughts?
> 
> Mackenzie
> 
>   def replace_with(map, key, _) when not is_map_key(map, key), do: map
>   def replace_with(map, key, f) do
>     Map.replace(map, key, f.(map[key]))
>   end
> -- 
> 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 elixir-lang-core+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/elixir-lang-core/aace8ef8-3566-4637-afb5-56bfb2f3e626n%40googlegroups.com.

-- 
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 elixir-lang-core+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/B1DA2E8B-F2CC-47FB-BA4F-89B2B1CD9CF7%40gmail.com.

Reply via email to