Let's say that I have a list of structs, with an id
as one of its keys:
users = [
%User{id: 1, attempts: 5},
%User{id: 2, attempts: 12},
%User{id: 3, attempts: 2}
]
What would the best way (the most elixir way) to update the number of attempts to 99 of the user with id == 2
to get a new users list:
updated_users = [
%User{id: 1, attempts: 5},
%User{id: 2, attempts: 99},
%User{id: 3, attempts: 2}
]
Thanks for helping!
Enum.map(users, fn
%User{id: 2} = user -> %User{user | attempts: 99}
user -> user
end)
You can encapsulate it into some module and give it nice name :)
Arguably, most elixirish approach would be to use Access
behaviour and Kernel.put_in/3
:
put_in users,
[Access.filter(&match?(%User{id: 2}, &1)),
Access.key(:attempts)],
99
#⇒ [
# %User{attempts: 5, id: 1},
# %User{attempts: 99, id: 2},
# %User{attempts: 2, id: 3}
#]
This might be easily extended to update multiple elements by changing the filter function.
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