walk vs map for processing a seq
clojure
Solution
Applying a function to a seq is the job of map. Use walk when you have to traverse both through and recursively into the entire structure.
Some examples of `walk`s may be found at ClojureDocs, also available at the REPL, e.g. `(user/clojuredocs clojure.walk/postwalk)`. Many of the examples are pedagogic and could and should be done with `map` or `for` (and sometimes `reduce`) in practice.
The typical use case for a `walk` is when you have a nested structure that you wish to process recursively. Some examples where this might be useful are the `clojure.walk` namespace itself, e.g. look at `(source clojure.walk/keywordize-keys)`. [Note, if you want to process it iteratively or at will, use zippers (or `tree-seq` for some simpler iterative cases).]
Another example that comes to mind is interpreting parse trees:
(require '[clojure.walk :as w])
(def t [+ [* [- 6 2] [/ 9 3]] [* 2 [+ 7 8]]])
(w/postwalk #(if (and (coll? %) (fn? (first %))) (apply (first %) (next %)) %) t)
;=> 42
Perhaps useful if, e.g., replacing `fn?` with an `allowed-fn?`, etc. to evaluate an edn expression, instead of invoking the too powerful eval compiler:
(eval t) ;=> [#<core$_PLUS_ ... ]
Oops, forms are lists, not vectors:
(def s (w/postwalk #(if (coll? %) (apply list %) %) t))
s ;=> (#<core$_PLUS_ ... )
(eval s) ;=> 42
Ah, notice here another use of a `walk` -- recursively changing the structure from nested vectors to nested lists.
An iterative example to meditate upon:
(require '[clojure.walk :as w])
(def s1 (range 8))
s1 ;=> (0 1 2 3 4 5 6 7)
(map inc s1)
;=> (1 2 3 4 5 6 7 8)
(w/postwalk #(if (number? %) (inc %) %) s1)
;=> (1 2 3 4 5 6 7 8)
(def s2 (partition 2 s1))
s2 ;=> ((0 1) (2 3) (4 5) (6 7))
(map (partial map inc) s2)
;=> ((1 2) (3 4) (5 6) (7 8))
(w/postwalk #(if (number? %) (inc %) %) s2)
;=> ((1 2) (3 4) (5 6) (7 8))
(def s3 (partition 2 s2))
s3 ;=> ((0 1) (2 3) (4 5) (6 7))
(map (partial map (partial map inc)) s3)
;=> (((1 2) (3 4)) ((5 6) (7 8)))
(w/postwalk #(if (number? %) (inc %) %) s3)
;=> (((1 2) (3 4)) ((5 6) (7 8)))
(def s4 (partition 2 s3))
s4 ;=> ((((0 1) (2 3)) ((4 5) (6 7))))
(map (partial map (partial map (partial map inc))) s4)
;=> ((((1 2) (3 4)) ((5 6) (7 8))))
(w/postwalk #(if (number? %) (inc %) %) s4)
;=> ((((1 2) (3 4)) ((5 6) (7 8))))
Problem
As I understand walk and map both apply a function to a seq. (walk also allows the application of an `outer` function post processing). However what are the idiomatic cases of using one over the other ?