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.

owaisqayum

owaisqayum

Enum.reduce is extremely powerful and it has reduced the code to just one line.

kokolegorille

kokolegorille

Yes, keyword new is not required :slight_smile:

BTW parens are not required too…

iex> [:a, :b, :c, :d, :e, :f] |> Enum.reverse() |> Enum.reduce([true], &[{&1, &2}])

Where Next?

Popular in Questions Top

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
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
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
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
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

We're in Beta

About us Mission Statement