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
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}]))
4
Also Liked
gregvaughn
Perhaps a bit
ish, but I’ve never actually used foldr before, so I put this together ![]()
iex(40)> :lists.foldr(&[{&1, &2}], [true], [:a, :b, :c, :d, :e, :f])
[a: [b: [c: [d: [e: [f: [true]]]]]]]
4
eksperimental
You have List.foldr/3 if you want to make it more Elixirish.
3
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.
2
Popular in Questions
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
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
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> somethi...
New
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
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
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
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
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
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
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
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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
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
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
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
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
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
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
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
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









