owaisqayum

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

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 :neutral_face:

Also Liked

stefanchrobot

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

owaisqayum

Yeah it makes sense now. max_by will output the only instance maximum output. Enum.reduce seems like a better option.

RudManusachi

RudManusachi

To be honest, if Enum.reduce is a better option or not - I’m not sure :grinning_face_with_smiling_eyes:

I like the example of @stefanchrobot more, because it looks simpler and reads better =)

Last Post!

RudManusachi

RudManusachi

To be honest, if Enum.reduce is a better option or not - I’m not sure :grinning_face_with_smiling_eyes:

I like the example of @stefanchrobot more, because it looks simpler and reads better =)

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement