我有一个嵌套的reduce函数,我有一个名为counter的原子在每次满足条件时递增一个计数
(defrecord TreeNode [val left right])
(defn build-tree [node xs]
(let [counter (atom 1)]
(reduce (fn [t x]
(reduce (fn [t l]
(if-not (= (:val l) -1)
(let [next-branch (nth xs @counter)]
(swap! counter inc)
;; do stuff
) t)
)t (map-indexed
(fn [idx itm]
(let [side (if (= 0 idx) :left :right)]
{:side side :index idx :val itm})) x))) node xs)))
我对在函数中使用可变参考感到不满意。有没有办法在不使用ref的情况下实现相同的行为?