Quantcast
Channel: clojure - refactor from atom into immutable - Stack Overflow
Viewing all articles
Browse latest Browse all 2

clojure - refactor from atom into immutable

$
0
0

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?


Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>