I play a little with atoms in clojure. I have a atompointing to lazy-seq. In another bit of code I want to update the value of the atom to the execution result nextin the sequence, but with the proviso that both swap!and reset!return the updated value, execution is never ending. I found that I could always transfer the call swap!, reset!in the do statement and then return nil, but I wonder how it is idiomatic or there is an alternative solution for this.
Doesn't end:
(def x (atom (range)))
(swap! x next)
Completes
(def x (atom (range)))
(do (swap! x next) nil)
(first @x) ;1
(do (swap! x next) nil)
(first @x) ;2
source
share