mrkurt

mrkurt

Map over a map and create a new map?

Hello! I’m trying to get more comfy with idiomatic elixir, and it seems like one line anonymous fn {} -> ... end calls might not be ideal. Maybe they are, but I’m curious if there’s a cleaner way to do a map over a map and get another map.

Here’s what I have:

Enum.map(m, fn {k,v}-> {k, length(v)} end) |> Map.new

Is there a way to write this more concisely? At first I thought capture syntax might be the way, but I can’t find a function to capture that creates a tuple.


And for bonus points, can you destructure capture arguments? Because I also have one of these guys:
|> Enum.flat_map(fn {_, %{metas: metas}} -> metas end)

—edit—
For the second example, I came up with this (which does indeed make me happier):

|> Map.values
|> Enum.flat_map(&Map.get(&1, :metas))

Marked As Solved

sabiwara

sabiwara

Elixir Core Team

Actually Map.new/2 accepts a function, so you can also directly call Map.new(m, fn {k,v}-> {k, length(v)} end).

12
Post #6

Also Liked

kokolegorille

kokolegorille

What about skipping the first Enum.map?

Map.new(m, & {elem(&1, 0), String.length(elem(&1, 1))})

You can also use for with reduce.

for {k, v} <- m, reduce: %{} do acc -> Map.put(acc, k, String.length(v)) end

or Enum.reduce directly

Enum.reduce(m, %{}, fn {k, v}, acc -> ... end)
kokolegorille

kokolegorille

You might not have seen I used Map.new/2 in my reply :slight_smile:

sabiwara

sabiwara

Elixir Core Team

Oh sorry I missed it!

Also worth noting, Map.new/2 should be the fastest option as well according to this benchmark.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
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
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement