I wanted to get peoples opinions on strategies for transforming, say, a list of maps based on some nested collection.
Take this data for example:
groups = [
%{group: 1, members: [%{name: "John", active: true}, %{name: "Jill", active: false}]},
%{group: 2, members: [%{name: "Rob", active: false}, %{name: "Eli", active: false}]},
%{group: 3, members: [%{name: "Janice", active: true}, %{name: "Sue", active: true}]}
]
A list of maps each with a nested list of maps (members
). Say I want a list of groups but without members that are active: false
AND filter out the groups that end up having no active members.
After taking a stab at trying to |>
functions I settled on a good ol’ list comprehension.
groups = (for group <- groups do
active_members = Enum.reject(group.members, fn(m) -> m.active == false end)
put_in(group, [:members], active_members)
end)
|> Enum.filter(fn(group) -> length(group.members) > 0 end)
IO.inspect(groups)
Which gives back what you would expect:
[%{group: 1, members: [%{active: true, name: "John"}]},
%{group: 3,
members: [%{active: true, name: "Janice"}, %{active: true, name: "Sue"}]}]
I tried for a while to make some kind of |>
transformation and couldn’t figure it out. Perhaps I was trying to hard to make something fit into the |>
style. Feel free to share your suggestions.
This got me thinking is there any good Cookbook style resources that can point you in the right direction for tackling certain problems in idiomatic Elixir? Thoughts?