chmod777

chmod777

Need help with this nested list

I have the following list:


[
  :juridical_person_document,
  [re_developments: [[properties: [[re_development: [:re_developer]]]]]],
  :legal_address
]

And my desired output would be this:

[
  :juridical_person_document, 
  re_developments: [properties: [re_development: 
  [:re_developer]]],
  :legal_address
]

In other words, I want to remove the unnecessary square brackets. How can I achieve that?

Marked As Solved

chmod777

chmod777

@al2o3cr Indeed, this is not valid syntax. Thanks for letting me know.

@dimitarvp I discovered that Macro.postwalk(list, fn [[x]] -> [x]; x -> x end) works for this case. But you’re right about the fact that I should learn how to handle this, I will take some time to study recursion better.

Also Liked

dimitarvp

dimitarvp

Welcome to the forum.

As @al2o3cr points out, your desired output is not even legal syntax. But if you put the atoms in front and the lists at the bottom then it becomes legal:

[
  :juridical_person_document,
  :legal_address,
  re_developments: [properties: [re_development: [:re_developer]]]
]

Note that this is actually a short-hand / convenient form of:

[
  :juridical_person_document,
  :legal_address,
  {:re_developments, [properties: [re_development: [:re_developer]]]}
]

Your original problem is solveable if you can do a recursive visit function that converts double-nested lists – e.g. [ [elements, ...] ] – to normal lists (e.g. [elements, ...]).

We can give you the solution if you are truly stuck, no worries, but it’s usually a good idea for you to learn. So what have you tried?

Secondly, avoid the XY problem and let us know the original problem you are grappling with that led to this conundrum. Maybe we’ll be able to help better.

al2o3cr

al2o3cr

[
  :juridical_person_document, 
  re_developments: [properties: [re_development: [:re_developer]]],
  :legal_address
]

As a literal, this isn’t valid syntax and makes the compiler complain:

** (SyntaxError) iex:18:67: unexpected expression after keyword list. Keyword lists must always come last in lists and maps. Therefore, this is not allowed:

    [some: :value, :another]
    %{some: :value, another => value}

Instead, reorder it to be the last entry:

    [:another, some: :value]
    %{another => value, some: :value}

Syntax error after: ','
    |
 18 |   re_developments: [properties: [re_development: [:re_developer]]],
    |                                                                   ^

The closest you can get is:

[
  :juridical_person_document, 
  {:re_developments, [properties: [re_development: [:re_developer]]]},
  :legal_address
]

It’s certainly possible to remove the extra lists wrapped around some elements, but I’d start by looking into where this data comes from - specifically, why every keyword list seems to end up with an extra list wrapped around it.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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

Other popular topics Top

New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement