Right now I’m a bit drowning in work but IIRC there already was a proposal for this, has anyone searched the mailing list?
On Wed, Mar 20, 2024, at 12:17 AM, Jean Klingler wrote: > I like it. It would be to `Access.filter` what `Enum.find` is to > `Enum.filter`. > I think it would be a nice addition as it can express operations that would > be quite verbose otherwise. > > Le mer. 20 mars 2024 à 02:30, 'oliver....@googlemail.com' via > elixir-lang-core <elixir-lang-core@googlegroups.com> a écrit : >> Hi. >> >> I already made a PR but was redirected here. ;-) >> >> This new function Access.find/1 would basically work like Enum.find/2 but >> for get_in/2 and similar functions. >> >> It can be used for scenarios like: >> - Popping the first found element. >> - Updating only the first found match in a list. >> - To get_in/2 an element directly instead of piping from get_in/2 into >> Enum.find/2. >> >> The implementation is very similar to Access.filter/1 and Access.at/1. >> >> We added this functions as utility function in our own project because we >> couldn't really find an elegant way to do such pointed updates with the >> existing functions. >> >> These are the examples I would have included in the doc string: >> >> iex> list = [%{name: "john", salary: 10}, %{name: "francine", salary: >> 30}] >> iex> get_in(list, [Access.find(&(&1.salary > 20)), :name]) >> "francine" >> >> iex> get_and_update_in(list, [Access.find(&(&1.salary <= 40)), >> :name], fn prev -> >> ...> {prev, String.upcase(prev)} >> ...> end) >> {"john", [%{name: "JOHN", salary: 10}, %{name: "francine", salary: >> 30}]} >> >> iex> list = [%{name: "john", salary: 10}, %{name: "francine", salary: >> 30}] >> iex> pop_in(list, [Access.find(&(&1.salary <= 40))]) >> {%{name: "john", salary: 10}, [%{name: "francine", salary: 30}]} >> >> iex> list = [%{name: "john", salary: 10}, %{name: "francine", salary: >> 30}] >> iex> get_in(list, [Access.find(&(&1.salary >= 50)), :name]) >> nil >> >> iex> get_and_update_in(list, [Access.find(&(&1.salary >= 50)), :name], >> fn prev -> >> ...> {prev, String.upcase(prev)} >> ...> end) >> {nil, [%{name: "john", salary: 10}, %{name: "francine", salary: 30}]} >> >> Thanks, >> Oliver >> >> >> -- >> 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/44ed5beb-1730-46d7-931a-217825cc4432n%40googlegroups.com >> >> <https://groups.google.com/d/msgid/elixir-lang-core/44ed5beb-1730-46d7-931a-217825cc4432n%40googlegroups.com?utm_medium=email&utm_source=footer>. > > > -- > 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/CANnyoha%2BwMRpTy_H2%3Dwy8sWjSQgXPpY-cbaL65Tx7D_AK7o1GA%40mail.gmail.com > > <https://groups.google.com/d/msgid/elixir-lang-core/CANnyoha%2BwMRpTy_H2%3Dwy8sWjSQgXPpY-cbaL65Tx7D_AK7o1GA%40mail.gmail.com?utm_medium=email&utm_source=footer>. -- 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/b07de549-4521-407f-84cd-83f5d3444ad2%40app.fastmail.com.