1.3.0 permalink Arrow_down_16x16
  • (walk inner outer form)
Traverses form, an arbitrary data structure. inner and outer are
functions. Applies inner to each element of form, building up a
data structure of the same type, then applies outer to the result.
Recognizes all Clojure data structures. Consumes seqs as with doall.

2 Examples top

  • (use 'clojure.walk)
    
    user=> (walk #(* 2 %) #(apply + %) [1 2 3 4 5])
    30
    
    user=> (walk second #(apply max %) [ [1 2] [3 4] [5 6] ])
    6
    
    user=> (walk first #(apply max %) [ [1 2] [3 4] [5 6] ])
    5
    
    user=> (walk first reverse [ [1 2] [3 4] [5 6] ])
    (5 3 1)
  • (require '[clojure.walk :as w])
    
    user=> (w/walk (fn [[k v]] [k (* 10 v)]) identity {:a 1 :b 2 :c 3})
    {:a 10, :c 30, :b 20}
    
    user=> (w/postwalk #(if (number? %) (* 2 %) %) [[1 2 3] [4 7 2] [2 5 2]])
    [[2 4 6] [8 14 4] [4 10 4]]
    
    user=> (let [s [1 '(2 3 [1])]] 
             (w/postwalk #(if (seq? %) (vec %) %) s))
    [1 [2 3 [1]]]
    
    user=> (w/walk (comp vec reverse) identity {0 :start 1 :inprogress 2 :end})
    {:start 0, :inprogress 1, :end 2}
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/walk.clj:35 top

(defn walk
  "Traverses form, an arbitrary data structure.  inner and outer are
  functions.  Applies inner to each element of form, building up a
  data structure of the same type, then applies outer to the result.
  Recognizes all Clojure data structures. Consumes seqs as with doall."

  {:added "1.1"}
  [inner outer form]
  (cond
   (list? form) (outer (apply list (map inner form)))
   (instance? clojure.lang.IMapEntry form) (outer (vec (map inner form)))
   (seq? form) (outer (doall (map inner form)))
   (coll? form) (outer (into (empty form) (map inner form)))
   :else (outer form)))
Vars in clojure.walk/walk:
Used in 0 other vars

Comments top

No comments for walk. Log in to add a comment.