shahryarjb
Hello friends, I have a list that I want to update it (3 level nested), and I need your suggestion to create better code
The list
elements = [
%{
children: [
%{
children: [
%{
children: [],
class: ["text-black", "w-full", "p-2"],
id: "c9ea0fff-1ee0-407e-8b2a-64afc954c40b",
index: 0,
parent: "section",
parent_id: "62197198-e3a1-46de-8f18-f2c6843f646f",
type: "text"
},
%{
children: [],
class: ["text-black", "w-full", "p-2"],
id: "b2a6b171-26e1-4e8a-8cd3-c41f388de6e8",
index: 1,
parent: "section",
parent_id: "62197198-e3a1-46de-8f18-f2c6843f646f",
type: "text"
}
],
class: ["flex", "flex-col", "justify-between", "items-stretch",
"min-h-[200px]", "w-full", "border", "border-dashed",
"border-gray-400", "p-1"],
id: "62197198-e3a1-46de-8f18-f2c6843f646f",
index: 0,
parent: "layout",
parent_id: "64a2c3f2-d71f-464a-8318-be072d7b6624",
type: "section"
}
],
class: ["flex", "flex-row", "justify-start", "items-center", "w-full",
"space-x-3", "px-3", "py-10"],
id: "64a2c3f2-d71f-464a-8318-be072d7b6624",
index: 0,
parent: "dragLocation",
parent_id: "dragLocation",
type: "layout"
}
]
for updating and adding tag to the text element I did like this, but it is very dirty I think:
def add_tag(elements, id, parent_id, layout_id, tag, type) when type in @elements do
Enum.map(elements, fn
%{type: "layout", id: ^layout_id, children: children} = selected_layout ->
edited_list =
children
|> Enum.map(fn
%{type: "section", id: ^parent_id, children: children} = selected_section ->
element_edited_list =
Enum.map(children, fn
%{type: ^type, id: ^id} = selected_element ->
Map.merge(selected_element, %{tag: tag})
element ->
element
end)
%{selected_section | children: element_edited_list}
section ->
section
end)
%{selected_layout | children: edited_list}
layout ->
layout
end)
end
MishkaTemplateCreatorWeb.MishkaCoreComponent.add_tag(elements, "c9ea0fff-1ee0-407e-8b2a-64afc954c40b", "62197198-e3a1-46de-8f18-f2c6843f646f", "64a2c3f2-d71f-464a-8318-be072d7b6624", "test1", "text")
By the way, for finding I created this code:
def find_element(elements, id, parent_id, layout_id, type) when type in @elements do
Enum.flat_map(elements, fn
%{type: "layout", id: ^layout_id, children: children} ->
case Enum.find(children, &(&1.id == parent_id)) do
nil ->
[]
%{type: "section", id: ^parent_id, children: children} ->
if is_nil(data = Enum.find(children, &(&1.id == id))), do: [], else: [data]
end
_layout ->
[]
end)
|> List.first()
end
Thank you in advance
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!
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’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
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
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
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
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
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
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
jegaxd26
Have you looked into Access.at/1 ?
pmangalakader
Not sure if this is what you’re looking for Floki - HTML Parser
This one makes it easier to handle HTML parsing.
Also, I wouldn’t prefer to nest too much
Enum.mapas, it makes code reading a bit trickier, normally I would use recursion for clean code if it is more than 2 nesting to make it easier for maintenance…Sebb
I’d try to find a better data-structure first.
Flattening is a good idea oftentimes.
Why is data and CSS mixed?
What are you trying to accomplish?
codeanpeace
The combination of the Access module with the Kernel functions update_in/3 and put_in/3 is very useful for updating nested data structures.
NduatiK
It looks like you might be doing a lot of data access.
You might get a huge boost in simplicity and performance by using maps instead of lists.
If you were willing to separate storing and sorting of children, you could store a map of
childrenand anorderlist that has ordered child ids:Once you do that, you can use @codeanpeace’s Access module functions at a cost of
O(n)instead ofO(n^3):Look up would be:
Updating an element
Removing an element:
Adding is similar to deleting
shahryarjb
@Sebb @pmangalakader
Repo: GitHub - mishka-group/mishka_template_creator: Mishka Template Creator for Phoenix and Phoenix LiveView · GitHub
Sebb
wow that looks really cool.
If I understand correctly you have an arbitrary nesting depth.
And you want to change an attribute in an arbitrary node, right?
So you can’t use a nested structure and a non-recursive algorithm to do that.
I would think about flattening the data in sth like
%{node_id => node}where each node has anidand a ref to the parent’sid(see the test below for an example).Here is some code I use to build a tree from a flat list:
shahryarjb
I think I will have a big problem with changing the order of keys in
children! when I use map Am I right?Because my user can change the order of elements in a section!
I think the only way to change the sort or re-order is regenerate new UUID, it is works when I have just 2 elements is a section more than 2 and change by side 2 element, again it is a problem
For example:
hst337
Just use Pathex. It is universal solution for nested structures
If you can describe what you’re trying to achieve in a human language, I can write you a pathex lens
hst337
I see you want to put tag into
type: "layout" ~> type: "section".With pathex this would be something like
You can learn how to create you own lenses with Pathex using these 5min tutorials: