shahryarjb

shahryarjb

Need advise to update nested list 3-level

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

Most Liked

jegaxd26

jegaxd26

Have you looked into Access.at/1 ?

hst337

hst337

Pathex is just like Elixir’s builtin Access, but structure-friendly, user-friendly, more performant and with some other cool features. They both use the same approach called “Functional optics”. Long story short, this approach is about writing fn functions which can set/get/update value in a structure (like getters or setter in OOP languages), and libraries like Pathex or Access are providing interface to create these functions and compose them together.

This approach is really useful and I suggest everyone to learn some optics, because it is universal way to traverse any nested structure. There are XPaths for XML, there are CSS selectors for HTML, there is dot-notation for structures, there is Access for primitive structures, but these approaches are format-specific, which is strange since everything in Elixir is represented as a combination of maps, lists and tuples. So, Pathex just leverages this feature and significantly reduces amount of stuff one must know to traverse nested data in Elixir. Just master Pathex and you can traverse XML, HTML, nested JSON or anything else using one tool.

So, there’s no magic, there are just functions, hehe

Last Post!

Sebb

Sebb

agree, only that it is most of the time way easier to work in a flat structure. May be a little slower to render, but will not be relevant in this use case.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
joaquinalcerro
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
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement