Adzz
Creating a list of paths from a Keyword list
I am super stuck on trying to get a list of paths from a keyword list. Let me give you some examples:
# This keyword list..
[:a, :b]
# Should produce this:
[[:a], [:b]]
# This keyword list..
[:a, b: [:c, d: :e]]
# Should produce this:
[
[:a],
[:b, :c],
[:b, :d, :e],
]
# This keyword list..
[:a, b: [:c, d: [:e]]]
# Should produce this:
[
[:a],
[:b, :c],
[:b, :d, :e],
]
The closest I’ve come is the following:
defmodule Path do
def reduce_over_path([{field, path}], acc, fun) do
reduce_over_path(path, fun.(field, acc), fun)
end
def reduce_over_path([field], acc, fun) do
fun.(field, acc)
end
def reduce_over_path(fields = [_|_], acc, fun) do
Enum.map(fields, fn
field ->
reduce_over_path(field, acc, fun)
end)
end
def reduce_over_path({field, rest}, acc, fun) do
reduce_over_path(rest, fun.(field, acc), fun)
end
def reduce_over_path(field, acc, fun) do
fun.(field, acc)
end
end
Path.reduce_over_path([:a, b: [:c, d: :e]], [], fn x, acc -> acc ++ [x] end)
# Returns this:
[
[:a],
[ [:b, :c], [:b, :d, :e] ]
]
The problem is the nested array.
Do I need to use Dijkstra’s Shortest Path Algorithm or something?
Most Liked
dimitarvp
You haven’t explained the algorithm you’re after. I see no clear correlation between input and output. What’s the goal?
2
NobbZ
Is just a list of atoms… “Keyword lists” are lists with binary tuples, first element beeing an atom, second element beeing an arbitrary value.
Also I’m not sure about the difference between your second and third example.
1
Popular in Chat/Questions
Hey,
What’s a good resource to learn Microservices in Elixir/Phoenix? Is there any book/docs/Github repos that I can refer to?
Thanks,
-M
New
Hello all,
I recently did my first app in Phoenix and Liveview, many thanks to all the users who assisted me. I found that the tutorial ...
New
and what resources you used to learn Elixir?
New
The title of this topic may sound silly at the first glance. But why I trying to ask you guys is how to go to the next level?
I am curre...
New
More Ecto questions! More madness!
The context: there’s a list of books that I want to filter with a dropdown…
The dropdown: looks some...
New
Hey all! Just joined the forum and I’m very excited to learn the language. I just finished reading the documentation and I was wondering ...
New
Hi all,
I am currently on this course
Half a way thru, and struggled a bit… sometimes I don’t even know what I am coding especially ...
New
Hi Everyone,
I am a student and know basics of web development, used some php and ruby, but I am not an expert in any. I want to learn E...
New
Hi sasajuric, any chance of a fresh promo code? :sunglasses:
New
It’s been a while since we asked this - I’m sure others (especially newcomers to the language) will be interested to hear how you’ve all ...
New
Other popular topics
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
Manning 2016 Halloween weekend sale via Deal of the Day
Friday, October 28 - Half off all MEAPs - code WM102816LT
Saturday, October 29 ...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
can someone please explain to me how Enum.reduce works with maps
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
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
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
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
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New







