fireproofsocks
I’m new to Elixir and functional programming, and I’m wondering if anyone has an example of how to perform a mapping operation on a hierarchical structure. The map represents a menu item, and each item may have multiple children of menu items, recursively.
This is tricky with your regular old OO code, but thinking of how to approach it with Elixir is making my brain melt.
The goal is to evaluate one attribute and use it to modify another. In this case, there’s sometimes an asterisk in the label, and if it is present, we strip it out of the label and set another field as a flag.
For example, this:
%{
"label" => "My Title*",
"has_footnote" => false
"children" => [...]
}
Becomes this:
%{
"label => My Title",
"has_footnote" => true
"children" => [...]
}
But recursively. Have I met my match? Is there some elegant way to do this, or is it something that only a ninja can pull off?
Thanks for any tips!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
gon782
Are you looking for something like this?
Note that you don’t have to match the “label” key or even the fact that it’s a map. I just did it so that I could use
titlewithout writingp.title, but the risk here is that if you have a badly formed collection of posts and some of them don’t have the"label"key, this actually acts as a filter, which is likely not what you want.The correct thing, of course, is to not be able to create a post struct like this without having these keys. You can do this using
defstructand the@enforce_keysmodule attribute (or using Vex, Ecto, etc. to have validation and required fields some other way):Edit: Of course @kokolegorille has it: You need to simply call the transformation on the list of children you have as well, and as long as they have the needed keys you’ll be fine.
kokolegorille
Maybe You want something more recursive, to process children as well?
kip
I find it more structured to separate the walking of the map (a “deep map”) from the transformation process. For example, I have a function called
deep_map/2that will recursively walk a map and invoke a function on each key-value pair. Its quite simple and documented at https://github.com/kipcole9/cldr/blob/master/lib/cldr/helpers/map.ex#L6-L44 I suspect many members of this forum have a similar function in their back pocket.Using this approach you would do the following:
(edited a bit to be clearer, not necessary exactly the required use case)
gon782
Here is a modification of @kokolegorille’s example, from an editor with some more structure:
I suggest studying @kip’s function in order to understand what is happening, as you’ll likely not have any issues with recursion as a concept if you fully understand it.
Edit: I’ve added an alternative approach using a
newfunction that could move the application of this logic to the creation of posts instead, provided you create them by calling that function.This, coupled with some discipline, is useful when you want to guarantee things about datatypes and is the preferred way in languages where the norm is to not expose raw data constructors and instead have limited interfaces generally only exposing constructor/modification/accessor functions.