Find and replace key’s value in deep nested map

Hello everyone, I have a deep nested map.

I need to format the values ​​across all “sum” keys.

How can I do that?

Deep nested map is always different.

Are there ways besides recursion during which we check the value of the key by type of whether the key is “sum” or not and checking the type of value of each element - by type if the value is a list or map, then we fall into yet another recursion.

You could write a tree zipper

I did one with this structure…

{left, right, focus, children, {left_parent, right_parent, parent_focus, parents}}

You can navigate the tree in constant time, and update in place…

If you have a recursive data structure, then you have to use recursion in order to handle it.
If you know the structure in advance, then update_in/3 can help

update_in(data, [:a, :b, :c, Access.all(), :d, :sum], &format_value/1)
1 Like