beatscode
Having an issue similar to https://forum.elixirforum.com/t/whats-the-best-way-to-access-and-retrieve-data-from-deeply-nested-maps-and-lists/613/4.
I have a data structure with a nested map I’d like to accumulate into a list. The problem is I don’t know how to recursively get the maps of nested elements.
Here is a contrived version. I’m trying to get a list of all the c and nested c values. Is there a way to do this?
test "Recursive Maps" do
#I just want all the c's in a list
rMap = %{:a => 1, :b => 2, :c => %{ :a => 111, :b => 2222,
:c => %{ :a => 212, :b => 323,
:c => %{:a => 45, :b => 524,
:c => %{}}}}}
rMap2 = %{:a => 3, :b => 4,
:c => %{ :a => 5, :b => 6,
:c => %{ :a => 7, :b => 8,
:c => %{:a => 9, :b => 10,
:c => %{a: 11, b: 12, c: 13}}}}}
rMaps = [rMap,rMap2]
IO.puts "rMaps List of Justcs"
IO.inspect rMaps
justCs = justc rMaps
IO.inspect justCs
justCs = justc3 rMaps
IO.inspect justCs
end
# Loop through list
def justc3(rMaps) do
Enum.reduce_while(rMaps, [], fn rMap, acc ->
if Map.has_key?(rMap,:c) == false do
{:halt, acc}
else
c = Map.get(rMap,:c)
{:cont, [c] ++ acc}
end
end)
end
def justc(rMaps) do
justCs = for rMap <- rMaps do
c = Map.get(rMap,:c)
# IO.inspect Enum.count(c)
if Enum.count(c) == 0 do
[]
else
Map.get(rMap,:c)
end
end
end
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
amnu3387
Why not just divide it into two functions that match on having a :c atom (if you want to match on binary keys too, then adding another function that matches on it) and recur, returning the accumulator if no map with :c is passed?
In case you want to customise at runtime the key that you dig for you could use a
case,idi527
What result do you want to get?
Running
justconrMapsreturnsfor me.
And
justc3onrMapsreturnsIs that what you want?
Or do you want to get
from
rMaps?Can
:cbe nested inside maps for keys other than:c, like in?
beatscode
Thank you @amnu3387, Not familiar with [c| acc] syntax. How is it that the acc variable gets accumulated to?
@idi527 I wanted to get every instance of :c in any of the maps and add them to a list
idi527
Can you provide an example? The exact result you want from
rMapsin your post.beatscode
The following is for each map but I’m figuring for rMaps I can write a comprehension to append to a parent list
amnu3387
I just had a little brainfart before, of course that won’t get you 'c’s inside other maps…
This will though, you match on the arguments that get passed to the reduce function. When you reduce a map you get a {k,v} tuple, so when you get a tuple where
vis a map, and the key matches the key you’re searching for, you recur the function, passing v (which is a map), and appending it to the accumulator. When you get a tuple where the value isn’t a map, but the key of the reduction is the key you’re looking for, you just append the value to the accumulator. When you get a tuple wherevis a map, but the key isn’t the one you want, you just recur to dig another level, but without appending to the accumulator. Otherwise you just return the accumulator as is, because it means thevisn’t a map (so no need to recur) and the key isn’t the key you want, so no need to append.Basically
[ h | t ]means, give me a list where you appendhto another listt(even if empty) and can be used as well to get theheadof a list (1 elem), and thetail(n elems) of it.