-
-
Save pbostrom/3251454 to your computer and use it in GitHub Desktop.
Transaction side effects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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