Adzz

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

dimitarvp

You haven’t explained the algorithm you’re after. I see no clear correlation between input and output. What’s the goal?

NobbZ

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.

Where Next?

Popular in Chat/Questions Top

meraj_enigma
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
shansiddiqui94
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
New
dopomecana
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
makeitrein
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
imanuelgittens
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
ericmachine88
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
asfand
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
New
AstonJ
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 Top

Brian
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
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
senggen
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
srinivasu
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
Tee
can someone please explain to me how Enum.reduce works with maps
New
ycv005
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
script
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
fayddelight
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
lucidguppy
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

We're in Beta

About us Mission Statement