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.

Reply via email to