Elixir provides a bunch of helper functions in Kernel that let you get and update values from a nested Map or another struct that implements Access:
get_in/2put_in/3update_in/3But I couldn't find anything that could help me delete a value. I know I could just update the value to nil (or just not be lazy by fetching the nested map, deleting the value and putting the whole map in), but I was hoping for a simple one liner that would actually delete the value that maybe I missed.
After googling a bit I decided to post this as a feature request in the official elixir mailing list, only to find out that it has already been requested, and an alternative does exist. It's Kernel.pop_in/2, which returns the deleted value and updated map in a tuple:
pop_in(%{user: %{name: "John", age: 27}, [:user, :age])
# => {27, %{user: %{name: "John"}}
José's reasoning for not having a separate delete_in/2 method:
The reason why we choose
pop_in/2is because it provides more features thandelete_inon its own and we want to avoid overloading Kernel with too many functions. If your concern is chaining, you can always performa |> elem(1)after popping.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With