我熟悉clojure.walk / prewalk和clojure.walk / postwalk
我想要像clojure.walk / prewalk和clojure.walk / postwalk这样的东西 - 但我也想要到达节点所需的路径 - 而prewalk / postwalk只给我节点,没有实际路径。
所以,如果我们有一个结构
[ {:cat "Garfield", :dog "DogeCoin"} [:a :b {:site "so"}]]
然后我想用args调用我的函数:
[] [ {:cat "Garfield", :dog "DogeCoin"} [:a :b {:site "so"}]]
[0] {:cat "Garfield", :dog "DogeCoin"}
[1] [:a :b {:site "so"}]
[0 :cat] "Garfield"
...
上面有内置的吗?处理函数接收节点和路径(从根节点)到节点的位置?
谢谢!
(基于fl00r的建议)
(defn pathwalk [f cur-path node]
(let [f1 #(pathwalk f (conj cur-path %1) %2)]
(f cur-path node)
(cond
(map? node) (map #(apply f1 %) node)
(or (vector? node) (list? node)) (keep-indexed f1 node))))
答案 0 :(得分:2)
我想你也希望'pathwalk'从函数f返回类似clojure.walk / prewalk的东西而不依赖于副作用? E.g。
(prewalk #(if (= :a %) :c %) [:a :b])
=>
[:c :b]
如果是,那么你可以这样做:
(defn pathwalk [f path e]
(let [e' (f path e)]
(cond
(map? e') (->> e'
(map (fn [[k x]] [k (pathwalk f (conj path k) x)]))
(into (empty e')))
(coll? e') (->> e'
(map-indexed (fn [i x] (pathwalk f (conj path i) x)))
(into (empty e')))
:else e')))
这是一个测试运行:
(pathwalk #(do
(println %1 %2)
(if (= :a %2)
:c
%2))
[]
[ {:cat "Garfield", :dog "DogeCoin"} [:a :b {:site "so"}]])
它会打印出来:
[] [{:cat Garfield, :dog DogeCoin} [:a :b {:site so}]]
[0] {:cat Garfield, :dog DogeCoin}
[0 :cat] Garfield
[0 :dog] DogeCoin
[1] [:a :b {:site so}]
[1 0] :a
[1 1] :b
[1 2] {:site so}
[1 2 :site] so
以下数据将从函数返回:
[{:cat "Garfield", :dog "DogeCoin"} [:c :b {:site "so"}]]