Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In Elixir, how to fix a nested list

In Elixir, I have the following list:

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

And my desired output would be this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[ :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?

I am aware that this can be achieved with recursion, but I didn’t figure out how yet.

>Solution :

All of a sudden, Macro.postwalk/2 would work here.

Macro.postwalk(input, fn [[x]] -> [x]; x -> x end)

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

This hack uses the fact that lists, atoms, and tuples of size two are all valid AST.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading