title: Clojure: apply a function to leaf nodes of a map
body: <p>How can you apply a function to leaf nodes of a (nested) map? For example, let's have this map:</p>
  
  <pre><code>{:a 0
   :b {:c 1}
   :d [{:e 2} {:f 3}]}
  </code></pre>
  
  <p>Let's say we want to increment all leaf nodes in this map and produce the following result:</p>
  
  <pre><code>{:a 1
   :b {:c 2}
   :d [{:e 3} {:f 4}]}
  </code></pre>
  
  <p>Currently, I'm thinking of using the map-zipper from <a href="https://stackoverflow.com/a/15020649/385505">this answer</a> and editing the map via the <code>clojure.zip</code> functions. However, I'm not sure how to iterate through the zipper and how to identify leaf nodes. What functions should I look at? Is there a simpler solution without zippers?</p>
  
  <p>Could something like the following work (provided there is a <code>leaf-node?</code> predicate testing if a location in zipper is a leaf node)?</p>
  
  <pre><code>(require '[clojure.zip :as zip])
  
  (defn inc-leaf-nodes
    [loc]
    (if (zip/end? loc)
      (zip/node loc)
      (recur (zip/next (if (leaf-node? loc)
                         (zip/edit loc inc)
                         loc)))))
  </code></pre>