You're viewing version 1.2.0 of walk. The latest stable version of Clojure Core is 1.3.0.
1.2.0 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 except sorted-map-by.
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 except sorted-map-by.
  Consumes seqs as with doall."
  {:added "1.1"}
  [inner outer form]
  (cond
   (list? form) (outer (apply list (map inner form)))
   (seq? form) (outer (doall (map inner form)))
   (vector? form) (outer (vec (map inner form)))
   (map? form) (outer (into (if (sorted? form) (sorted-map) {})
                            (map inner form)))
   (set? form) (outer (into (if (sorted? form) (sorted-set) #{})
                            (map inner form)))
   :else (outer form)))
Used in 0 other vars

Comments top

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