owaisqayum

owaisqayum

How can we create a nested keyword list

How can we create a nested keyword list from the following list. The list length can change with time.

list

[:a, :b, :c, :d, :e, :f]

how can we convert it to

[a: [b: [c: [d: [e: [f: [ true ] ]]]]]]

Is it possible with elixir as I tried to use recursion but it’s not working as expected.

Thanks

Marked As Solved

kokolegorille

kokolegorille

iex> l |> Enum.reverse() |> Enum.reduce([true], fn x, acc -> Keyword.new([{x, acc}]) end)
[a: [b: [c: [d: [e: [f: [true]]]]]]]

# or the short version...
iex> l |> Enum.reverse() |> Enum.reduce([true], &Keyword.new([{&1, &2}]))

Also Liked

gregvaughn

gregvaughn

Perhaps a bit :golf: ish, but I’ve never actually used foldr before, so I put this together :grin:

iex(40)> :lists.foldr(&[{&1, &2}], [true], [:a, :b, :c, :d, :e, :f])
[a: [b: [c: [d: [e: [f: [true]]]]]]]
eksperimental

eksperimental

You have List.foldr/3 if you want to make it more Elixirish.

kokolegorille

kokolegorille

When You have an Enumerable, and want to get one value… the usual suspect is Enum.reduce.

iex> l = [:a, :b, :c, :d, :e, :f]
iex> Enum.reduce l, [], fn x, acc -> Keyword.put([], x, acc) end   
# or
iex> Enum.reduce l, [], fn x, acc -> Keyword.new([{x, acc}]) end
[f: [e: [d: [c: [b: [a: []]]]]]]

This code does not solve your problem, but shows how to nest… I would reverse the list first, and add the final true to get it work.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement