owaisqayum
Iterating through a nested map without changing the structure
Hi,
I am having a map from which I want to extract values by the maximum length of values.
map =
%{
"A" => [
{:b, "B", 1},
{:b, "C", 1}
],
"B" => [
{:b, "D", 1}
],
"C" => [
{:b, "E", 1},
{:b, "F", 1}
]
}
Here is my code:
Enum.max_by(map, fn {key, value} -> length(value) end)
which gives me
{"A", [{:b, "B", 1}, {:b, "C", 1}]}
while I need to get a list of both key values that have maximum values. In this case, this should be the output
[
{"A", [{:b, "B", 1}, {:b, "C", 1}]},
{"C", [{:b, "E", 1}, {:b, "F", 1}]}
]
Marked As Solved
RudManusachi
Enum.max_by returns at max 1 element =)
If you want to do it in a single cycle you probably want to do some custom reduce like
Enum.reduce(map, [], fn
item, [] -> [item]
{_, val} = item, [{_, acc_val} | _] when length(val) > length(acc_val) -> [item]
{_, val} = item, [{_, acc_val} | _] = acc when length(val) == length(acc_val) -> [item | acc]
_, acc -> acc
end)
P. S. naming is hard ![]()
Also Liked
stefanchrobot
Seems that max_by won’t work for you here, because:
If multiple elements are considered maximal, the first one that was found is returned.
So you need something like:
{count, values} =
map
|> Enum.group_by(fn {_key, values} -> length(values) end)
|> Enum.max_by(fn {count, _values} -> count end)
owaisqayum
Yeah it makes sense now. max_by will output the only instance maximum output. Enum.reduce seems like a better option.
RudManusachi
To be honest, if Enum.reduce is a better option or not - I’m not sure ![]()
I like the example of @stefanchrobot more, because it looks simpler and reads better =)
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








