shahryarjb

shahryarjb

Find a specific string in an unstructured and nested list

Hi friends, I have a nested list that is unstructured, and I really know how much it has nested list or map in itself.

I just need to search and get all :tag key in this list and check the selected name by my user exists there or it is unique.

For example:

[
  %{
    children: [
      %{
        children: [],
        id: "fcfb1066-6e16-4a84-81e0-7746f12b7e76",
        tag: "test1",
        type: "section"
      },
      %{
        id: "035ca737-2cc2-488b-83f5-fe7cc2becc5e",
        parent_id: "a9963499-f6a4-4a32-bafa-aa22a9b059bc",
      },
      %{
        children: [
          %{
            other: [
              %{
                id: "035ca737-2cc2-488b-83f5-fe7cc2becc5e",
                parent_id: "a9963499-f6a4-4a32-bafa-aa22a9b059bc",
              },
            ],
            tag: "test4",
          },
        ],
        id: "8a9f0126-33a0-4c03-8c4a-5507f0562c8a",
        index: 2,
        parent: "layout",
        parent_id: "a9963499-f6a4-4a32-bafa-aa22a9b059bc",
        tag: "test2",
        type: "section"
      }
    ],
    parent: "dragLocation",
    parent_id: "dragLocation",
    tag: "test3",
  }
]

This list has no a structure that helps me to use Enum.

So I try to use macro, but based on my little knowledge I could not to find all :tag, see this code please

defmacro get_tags(data, selected_name) do
  quote do
    data = unquote(data)
    data_string = inspect(data, charlists: :as_strings)
    ast = Code.string_to_quoted(data_string)
    
    ....
  end
end

the ast = Code.string_to_quoted(data_string) passes me a nested list again, and I don’t know how to create a pattern to get tag, so after that I use to List.flatten ast but I got this error:
** (FunctionClauseError) no function clause matching in :lists.flatten/1

because It has maps in its list

Please help me to learn more about macro and search like this in a list, Thank you in advance.

Most Liked

tcoopman

tcoopman

I’m not very fluent with macros either. Having said that, I really don’t see how macros are going to help you with the problem at hand.
Macros are not something you should just reach to, they are powerful but also add complexity. So only use them when necessary.

Back to your problem:
If there are no children, or no tags, my solution handles that.
If the children is not named children, how do you know it’s something you should decent into?
What is the actual problem you’re trying to solve?

tcoopman

tcoopman

Macros are not magic. They won’t lower the complexity of the problem.
What I would do is write test cases for the different kinds of inputs you need to handle and then implement code for them.

hst337

hst337

Use Pathex

import Pathex.Combinator
import Pathex.Lenses
import Pathex

tag_lens =
  combine(fn rec ->
    path(:tag, :map) ||| (star() ~> rec)
  end)

Pathex.view(nested_stuff, tag_lens ~> matching("test2"))
tcoopman

tcoopman

I don’t understand why you want to use a macro to solve this.

What I’d suggest is a recursive search.

tcoopman

tcoopman

This is something that should work:

  def find([], tags), do: tags

  def find([hd | tl], tags) do
    tag = Map.get(hd, :tag)
    tags = if tag, do: [tag | tags], else: tags

    child = Map.get(hd, :children)
    tags = if child, do: find(child, tags), else: tags

    find(tl, tags)
  end

Where Next?

Popular in Questions Top

Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
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

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
malloryerik
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
vonH
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
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
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

We're in Beta

About us Mission Statement