Find and replace key's value in deep nested map

Hi,

I have a map

%{
   a: 
     %{
        b:
          %{
             find_me_1: "Hi" 
            }
      }
  }

I want to be able to find the key find_me_1 in the map and replace its value with “Bye”. I came across a few kernel functions like Kernel. put_in/3, Kernel. get_and_update_in/3. But looks like these functions work only when we are aware of the exact level of the map or when we know the path of the key. I have a scenario where the key can exist in any level of the map. Any suggestion to do this in an optimized way?

The only practical optimization for this I can think of is to keep an index of the map structure of some sort (which isn’t that efficient since you’d need to update the index each time you update the map). Your basic options without maintaining an index are depth-first traversal or breadth-first traversal. I think you’d implement it in a manner wherein the BFS or DFS function would pass the information you’d need to pass to the get_and_update_in function, maybe using the Stream module so that you could iterate until a condition is met.

If you’re able to store the data in an external datastore like Postgres, you might be able to leverage the jsonb GIN index to locate your item.