postwalk

added
1.1

ns
clojure.walk

type
function

(postwalk f form)

Performs a depth-first, post-order traversal of form.  Calls f on
each sub-form, uses f's return value in place of the original.
Recognizes all Clojure data structures. Consumes seqs as with doall.

                (use 'clojure.walk)
(let [counter (atom -1)]
   (postwalk (fn [x]
                [(swap! counter inc) x])
              {:a 1 :b 2}))

=> [6 {2 [[0 :a] [1 1]], 5 [[3 :b] [4 2]]}]
            
                ;;example of removing namespaces from all keys in a nested data structure
(def thing {:page/tags [{:tag/category "lslsls"}]})
(postwalk #(if(keyword? %)(keyword (name %)) %) thing)
{:tags [{:category "lslsls"}]}
            
                (use 'clojure.walk)

;;example of evaluating an expression tree, starting at the leaves
(def expression-tree
  {:function +
   :children
    [1 {:function *
        :children [2 6]}]})

(defn evaluate [node]
  (if-let [f (:function node)]
    (apply f (:children node))
    node))

(postwalk evaluate expression-tree)

=> 13