I am playing a bit with atoms in clojure. I have an atom
pointing at a lazy-seq
. In another bit of code I want to update the value of the atom to the result of doing next
on the sequence, but given that both swap!
and reset!
return the updated value execution never ends.
I figured out that I could always wrap the call to swap!
, reset!
in a do statement and then return nil
, but I am wondering how idiomatic this is or whether there is an alternative solution to do it.
Doesn't terminate:
(def x (atom (range)))
(swap! x next)
Terminates
(def x (atom (range)))
(do (swap! x next) nil)
(first @x) ;1
(do (swap! x next) nil)
(first @x) ;2