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 =)

Where Next?

Popular in Questions Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement