Skip to content

Instantly share code, notes, and snippets.

@llasram
Forked from pbostrom/gist:3250162
Created August 3, 2012 20:34
Show Gist options
  • Save llasram/3251293 to your computer and use it in GitHub Desktop.
Save llasram/3251293 to your computer and use it in GitHub Desktop.
Transaction side effects
(let [a (agent nil), q (ref []), x (ref 0)]
(defn run-side-effects [_]
(let [fs (dosync
(let [q' @q]
(ref-set q [])
q'))]
(doseq [f fs] (f))))
(defn alter-and-order-side-effects []
(dosync
(let [newx (alter x inc)]
(alter q conj (fn [] (println "x is" newx)))
(send a run-side-effects)))))
(dorun (map future-call (repeat 10 alter-and-order-side-effects)))
;; x is 1
;; x is 2
;; x is 3
;; x is 4
;; x is 5
;; x is 6
;; x is 7
;; x is 8
;; x is 9
;; x is 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment