817b4688d8
The hy.contrib.walk module provides a few functions to walk the Hy AST, and potentially transform it along the way. The main entry point is (walk), which takes two functions and a form as arguments, and applies the first (inner) function to each element of the form, building up a data structure of the same type as the original. Then applies outer (the second function) to the result. Two convenience functions are provided: (postwalk) and (prewalk), which do a depth-first, post/pre-order traversal of the form. Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
25 lines
659 B
Hy
25 lines
659 B
Hy
(import [hy.contrib.walk [*]])
|
|
|
|
(def walk-form '(print {"foo" "bar"
|
|
"array" [1 2 3 [4]]
|
|
"something" (+ 1 2 3 4)
|
|
"quoted?" '(foo)}))
|
|
|
|
(defn collector [acc x]
|
|
(.append acc x)
|
|
nil)
|
|
|
|
(defn test-walk-identity []
|
|
(assert (= (walk identity identity walk-form)
|
|
walk-form)))
|
|
|
|
(defn test-walk []
|
|
(let [[acc '()]]
|
|
(assert (= (walk (partial collector acc) identity walk-form)
|
|
[nil nil]))
|
|
(assert (= acc walk-form)))
|
|
(let [[acc []]]
|
|
(assert (= (walk identity (partial collector acc) walk-form)
|
|
nil))
|
|
(assert (= acc [walk-form]))))
|