beatscode

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

Showing Posts 1 to 6

amnu3387

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?

defmodule Test do
  def get_c(acc, %{:c => c}), do: Test.get_c([c | acc], c)
  def get_c(acc, _), do: acc
end

iex(4)> defmodule Test do
...(4)> def get_c(acc, %{:c => c}), do: Test.get_c([c | acc], c)
...(4)> def get_c(acc, _), do: acc
...(4)> end

{:module, Test,
 <<70, 79, 82, 49, 0, 0, 3, 248, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 85,
   0, 0, 0, 9, 11, 69, 108, 105, 120, 105, 114, 46, 84, 101, 115, 116, 8, 95,
   95, 105, 110, 102, 111, 95, 95, 9, 102, ...>>, {:get_c, 2}}

iex(5)> Test.get_c([], %{a: 'test', c: %{a: false, b: true, c: %{c: 1, b: 2, d: 3}}})

[1, %{b: 2, c: 1, d: 3}, %{a: false, b: true, c: %{b: 2, c: 1, d: 3}}]

In case you want to customise at runtime the key that you dig for you could use a case,

def get_arbitrary(acc, %{} = map, key) do
  case map[key] do
    nil -> acc
    value -> get_arbitrary([value | acc], value, key)
  end
end
def get_arbitrary(acc, _, _), do: acc
idi527

idi527

What result do you want to get?

Running justc on rMaps returns

[
  %{a: 111, b: 2222, c: %{a: 212, b: 323, c: %{a: 45, b: 524, c: %{}}}},
  %{a: 5, b: 6, c: %{a: 7, b: 8, c: %{a: 9, b: 10, c: %{a: 11, b: 12, c: 13}}}}
]

for me.

And justc3 on rMaps returns

[
  %{a: 5, b: 6, c: %{a: 7, b: 8, c: %{a: 9, b: 10, c: %{a: 11, b: 12, c: 13}}}},
  %{a: 111, b: 2222, c: %{a: 212, b: 323, c: %{a: 45, b: 524, c: %{}}}}
]

Is that what you want?

Or do you want to get

[nil, 13]

from rMaps?

Can :c be nested inside maps for keys other than :c, like in

%{a: %{c: :c}, c: %{...: ...}}

?

beatscode

beatscode OP

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

idi527

Can you provide an example? The exact result you want from rMaps in your post.

beatscode

beatscode OP

The following is for each map but I’m figuring for rMaps I can write a comprehension to append to a parent list

[
  %{},
  %{a: 45, b: 524, c: %{}},
  %{a: 212, b: 323, c: %{a: 45, b: 524, c: %{}}},
  %{a: 111, b: 2222, c: %{a: 212, b: 323, c: %{a: 45, b: 524, c: %{}}}}
]
amnu3387

amnu3387

I just had a little brainfart before, of course that won’t get you 'c’s inside other maps…

defmodule Test do
  def get_cees(map, acc, key) do
    Enum.reduce(map, acc, fn 
      ({k, %{} = v}, acc) when k == key -> Test.get_cees(v, [ v | acc], key)
      ({k, v}, acc) when k == key -> [v | acc]
      ({k, %{} = v}, acc) -> Test.get_cees(v, acc, key)
      (_ , acc) -> acc
      end)
   end
 end

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 v is 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 where v is 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 the v isn’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 append h to another list t (even if empty) and can be used as well to get the head of a list (1 elem), and the tail (n elems) of it.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews