Search code examples
recursionclojurespecter

Recursively mutating values in a map of maps with Specter


complete Clojure newbie here. This is a simple question, but I cant seem to get it:

Given that I have a nested hasmap of unknown depth, how do I use Specter's transform() to mutate the values of the data structure? I imagine a recursive-path is required here, but I cant get it to work. a working example is what I'm after and unfortunately, there isn't one in the docs.

(there is an example for set-val on a recursive map, but I don't know how to transform that into a transform use-case)

EDIT: More details were requested, so here they are: I'm interested in a transform form that can mutate all the values of a nested maps - of any depth). for example, this transform would be able to increment all the values in the following maps (and any other nested map):

{:a 1 :b {:c 2 :d {:e 3}}} 
AND
{:a 1 :b {:c 2}}
AND
{:a 1}

the line of code I'm interested in might look sth like this:

(transform <missing selector here> inc data)

Solution

  • Using the linked example:

    (def MAP-NODES
       (recursive-path [] p
         (if-path map?
           (continue-then-stay MAP-VALS p))))
    
    (transform [MAP-NODES MAP-VALS number?] inc data)