Currently, Elixir provides a number of functions that make working with nested data structures very easy and pleasant:
get_in/2
get_and_update_in/2
and get_and_update_in/3
update_in/2
and update_in/3
put_in/2
and put_in/3
pop_in/1
and pop_in/2
Unfortunately, there’s no similar function for deleting a value from a nested structure. Is its omission intentional? I think a delete_at
function would be very useful:
data = %{users: [%{name: "John", age: 27}, %{name: "Betty", age: 40}]}
delete_at(data, [:users, Access.all(), :age])
# => %{users: [%{name: "John"}, %{name: "Betty"}]}
I imagine it would also make sense to provide a delete_at/1
macro that allows the path to be passed using access syntax, just like what put_in/2
, pop_in/1
, etc provide.
Thoughts?
Myron
--
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-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/1d4e99a5-1b9c-4bd6-8313-a55484639a87%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
pop_in
works, but it’s not very friendly for pipelines. For example, let’s say I want to do this:
%{users: [%{name: "John", age: 27}, %{name: "Betty", age: 40
}]}
|> delete_at([:users, Access.all(), :age])
|> update_in([:users, Access.all(), :name], &String.downcase/1)
# => %{users: [%{name: "john"}, %{name: "betty"}]}
If pop_in
is the only option, I have to break up my pipeline (using an expression like {_, deleted} = pop_in(...)
to discard the popped value) or add an elem(1)
to my pipeline. IMO, both options make the code far less clear than it would be if delete_at
is available.
Also, if the answer is “delete isn’t needed because pop_in exists”, then why do Map
and Keyword
provide delete
and pop
operations? The same argument could be made there that delete
is unnecessary since they provide pop
but I’m glad delete
is necessary for them and would like it for nested structures, too.
--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/Rw6TLUoNZ0w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4JUJMKaD3-zLi7_c-wgdFD-hh7LbvHK7y0cXv11Sjkw0w%40mail.gmail.com.
%{users: [%{name: "John", age: 27}, %{name: "Betty", age: 40}]}
|> pop_in([:users, Access.all(), :age])|> elem(1)
|> update_in([:users, Access.all(), :name], &String.downcase/1)# => %{users: [%{name: "john"}, %{name: "betty"}]}
Also, if the answer is “delete isn’t needed because pop_in exists”, then why do
Map
andKeyword
providedelete
andpop
operations? The same argument could be made there thatdelete
is unnecessary since they providepop
but I’m gladdelete
is necessary for them and would like it for nested structures, too.
--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/Rw6TLUoNZ0w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4LjeiJsNOEkRr_c1aoUxiY6mfQM%3DJ3mqE5EVNv1OonTCw%40mail.gmail.com.