我在编写可以遍历异构节点树的 zipper 时遇到问题。我有 i) map 列表。每个映射都有 ii) 一个 :inputs 键,其值是映射列表。
我想使用 zipper 来访问每一片叶子并添加一个计算值。代码块和输入树数据如下。我似乎无法获取访问叶节点的 zipper 代码,并添加 { :thing 123 }。我一定错过了一些简单的东西。有什么见解吗?
一)
(loop [loc (zip/zipper (or map? list?) #((if (map? %1) ;; get children of a node (:inputs %1) %1 )) #(%1) ;; create a new node (:input-layer neural-network)) ] (if (zip/end? loc) (zip/root loc) (if (map? loc) (recur (zip/next (zip/edit loc merge { :thing 123 } ))) (recur (zip/next loc)) ) ) )
B)
({:inputs ({:key :avolume, :value 2.25, :weight 0.4283380545172636, :bias 0} {:key :bvolume, :value 3.0, :weight 0.6970037374879661, :bias 0} {:key :ask, :value 1.32379, :weight 0.5387437158323669, :bias 0} {:key :bid, :value 1.3239, :weight 0.4648184032361037, :bias 0} {:key :time, :value 1.335902400676, :weight 0.43632873607404554, :bias 0}), :id "583c10bfdbd326ba34aed329139df6be2487ffc"} {:inputs ({:key :avolume, :value 2.25, :weight 0.13162215440222336, :bias 0} {:key :bvolume, :value 3.0, :weight 0.23886901184352727, :bias 0} {:key :ask, :value 1.32379, :weight 0.8408470512339872, :bias 0} {:key :bid, :value 1.3239, :weight 0.27071013797961796, :bias 0} {:key :time, :value 1.335902400676, :weight 0.6304505838898373, :bias 0}), :id "583c10bfdbd326ba34aed329139df6be2487ffd"} {:inputs ({:key :avolume, :value 2.25, :weight 0.8236972641966921, :bias 0} {:key :bvolume, :value 3.0, :weight 0.32421537754016705, :bias 0} {:key :ask, :value 1.32379, :weight 0.966306328543246, :bias 0} {:key :bid, :value 1.3239, :weight 0.8891668220470931, :bias 0} {:key :time, :value 1.335902400676, :weight 0.278993745549462, :bias 0}), :id "583c10bfdbd326ba34aed329139df6be2487ffe"} {:inputs ({:key :avolume, :value 2.25, :weight 0.27388486254027167, :bias 0} {:key :bvolume, :value 3.0, :weight 0.33659579299487363, :bias 0} {:key :ask, :value 1.32379, :weight 0.16610378593177033, :bias 0} {:key :bid, :value 1.3239, :weight 0.6964784902474896, :bias 0} {:key :time, :value 1.335902400676, :weight 0.6306732906337643, :bias 0}), :id "583c10bfdbd326ba34aed329139df6be2487fff"} {:inputs ({:key :avolume, :value 2.25, :weight 0.8819171698935051, :bias 0} {:key :bvolume, :value 3.0, :weight 0.5944805362120958, :bias 0} {:key :ask, :value 1.32379, :weight 0.9060962647355373, :bias 0} {:key :bid, :value 1.3239, :weight 0.37647418075176464, :bias 0} {:key :time, :value 1.335902400676, :weight 0.7797681719480866, :bias 0}), :id "583c10bfdbd326ba34aed329139df6be2488000"})
谢谢
最佳答案
我认为创建新节点的 zipper 部分是不正确的。如果您阅读definition of zipper function ,您将看到使用 2 个参数调用 make-node
:当前节点、新子节点的序列。函数应该返回新节点。
您使用的表达式#(%1)
根本不起作用,因为您尝试将 map 或列表作为函数调用。
此外,表达式(或 map ?列表?)
并不能真正实现您想要的功能。
我将其改编为向量只是为了测试。
(defn mk-zip [root] (let [branch? (fn [node] (when node (or (and (map? node) (contains? node :inputs)) (vector? node)))) children (fn [node] (cond (nil? node) nil (map? node) (:inputs node) :else node)) make-node (fn [node children] (cond (nil? node) nil (map? node) (assoc node :inputs children) (vector? node) (into [] children) :else node))] (zip/zipper branch? children make-node root))) (def root [{:inputs [{:key :avolume, :value 2.25, :weight 0.4283380545172636, :bias 0} {:key :bvolume, :value 3.0, :weight 0.6970037374879661, :bias 0} {:key :ask, :value 1.32379, :weight 0.5387437158323669, :bias 0} {:key :bid, :value 1.3239, :weight 0.4648184032361037, :bias 0} {:key :time, :value 1.335902400676, :weight 0.43632873607404554, :bias 0}], :id "583c10bfdbd326ba34aed329139df6be2487ffc"}])
Updating map items now works:
(def z (mk-zip root))
(-> z zip/next zip/down
(zip/insert-right {:key :new :value -10})
zip/up
zip/node)
打印:
{:inputs ({:key :avolume, :weight 0.4283380545172636, :bias 0, :value 2.25} {:key :new, :value -10} {:key :bvolume, :weight 0.6970037374879661, :bias 0, :value 3.0} {:key :ask, :weight 0.5387437158323669, :bias 0, :value 1.32379} {:key :bid, :weight 0.4648184032361037, :bias 0, :value 1.3239} {:key :time, :weight 0.43632873607404554, :bias 0, :value 1.335902400676}), :id "583c10bfdbd326ba34aed329139df6be2487ffc"}
关于异构树上的 Clojure Zipper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12501609/