I have a nested reduce function and I amusing an atom named counter to increment a count every time a condition is met
(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)))
I'm not happy about using a mutable ref in the function. Is there a way I could achieve the same behaviour without using the ref?