n4thyra
There are similar questions such as Build a Tree of Structs but I haven’t found any that would deal with exactly the same issue that I need to solve.
Let’s say we have an (unsorted) set of data that we load from DB
[
%{id: 2, name: "Child 2", parent_id: nil},
%{id: 1 ,name: "Child 1", parent_id: nil},
%{id: 3, name: "GrandChild 1", parent_id: 1},
%{id: 5, name: "Child 3", parent_id: nil},
%{id: 6, name: "GrandGrandChild 1", parent_id: 3},
]
Desired result
[
%{
id: 2,
name: "Child 2",
children: [],
},
%{
id: 1,
name: "Child 1",
children: [
%{
id: 3,
name: "GrandChild 1",
children: [
%{
id: 6,
name: "GrandGrandChild 1",
children: []
},
]
},
],
},
%{
id: 5,
name: "Child 3",
children: [],
},
]
There can be N levels, not just 3
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
I would start by selecting root nodes, where parent_id is nil. Then use a recursive constructor to build children’s field.
n4thyra
Thanks a lot!
It didn’t work inside a module, so I’ve turned your piece of code into this and it is now working like a charm.
kokolegorille
It’s because the anonymous function is using list as a closure…
BTW You can optimize by passing a partial list with root nodes removed, as they are not going to be child of anything.
stefanchrobot
You could use
digraphanddigraph_utilsto do this.dimitarvp
Curious how exactly btw, got a snippet handy?
n4thyra
I’ve googled but haven’t found much, could you show us how to use it?
al2o3cr
Enum.group_byis a little more efficient than repeatedly scanning the whole list withEnum.filter, and (IMO) is easier to understand:The key here is transforming the input into a map that can answer the question “what are the nodes that have a given
parent_id”:stefanchrobot
Here’s how you can do it with
:digraph:The idea here is that the input represents the vertices (
idandname) and the edges (parent_id). I introduced a fake root node since working with a tree is easier. I first build the graph/tree and then traverse it. Since:digraphis based on ETS, the code does not need to carry the state around.