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.


















