scandingo
I’m new to functional programming and I’d like to know the idiomatic way to convert a flat list of maps into a nested list.
For some background, I’m experimenting with Ecto. I have a table with a recursive relationship to itself. That is to say that there is a parent_id field which has a foreign key pointing to the id field of the same table. I’m trying to get a list of just the rows where the parent_id is null at the top level with all of its children in a nested list.
flat = [
%{id: 1, name: "region 1", parent_region_id: nil},
%{id: 2, name: "subregion 1", parent_region_id: 1},
%{id: 3, name: "subregion 2", parent_region_id: 1},
%{id: 4, name: "region 2", parent_region_id: nil},
%{id: 5, name: "subregion 3", parent_region_id: 4},
%{id: 6, name: "subregion 4", parent_region_id: 4}
]
nested = [
%{
id: 1,
name: "region 1",
parent_region_id: nil,
subregions: [%{id: 2, name: "subregion 1", parent_id: 1}, %{id: 3, name: "subregion 2", parent_region_id: 1}]
},
%{
id: 4,
name: "region 2",
parent_region_id: nil,
subregions: [%{id: 5, subregion: "subregion 3", parent_region_id: 4}, %{id: 6, name: "subregion 4", parent_region_id: 4}]
}
]
I can achieve this by using Repo.preload(:subregions), but that would add an unnecessary join. Any tips on an elegant way to convert flat into nested?
Here is the migration script.
defmodule Bazaar.Repo.Migrations.CreateRegions do
use Ecto.Migration
def change do
create table(:regions) do
add :name, :string
add :parent_region_id, references(:regions), null: true
timestamps()
end
end
end
And this is the schema.
defmodule Bazaar.Geoscheme.Region do
use Ecto.Schema
import Ecto.Changeset
schema "regions" do
field(:name, :string)
belongs_to(:parent_region, Bazaar.Geoscheme.Region)
has_many(:subregions, Bazaar.Geoscheme.Region, foreign_key: :parent_region_id)
timestamps()
end
@doc false
def changeset(region, attrs) do
region
|> cast(attrs, [:name, :parent_region_id])
|> validate_required([:name])
end
end
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
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
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Sebb
al2o3cr
The key step here is splitting the calculation into two parts:
Enum.group_bythat pulls together thesubregionslistsscandingo
Awesome! This worked as is when piping my
Repo.allinto it. Now let’s see if I actually understand it.Is the following correct?
This bit
& &1.parent_region_idcreates an anonymous function and returns the parent_region_id of the first argument which is then used to group the map elements.Everything else seems comprehensible.
benwilson512
That is 100% correct!