Skip to content

Instantly share code, notes, and snippets.

@pbostrom
Forked from llasram/gist:3251293
Created August 3, 2012 20:56
Show Gist options
  • Save pbostrom/3251454 to your computer and use it in GitHub Desktop.
Save pbostrom/3251454 to your computer and use it in GitHub Desktop.
Transaction side effects
(let [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))))
(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