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