Created
December 27, 2011 00:08
-
-
Save greghelton/1522324 to your computer and use it in GitHub Desktop.
Clojure: use recur & use Clojure transactiopn
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
; refs manage synchronous, coordinated state | |
; agents manage asynchronous, independent state | |
; atoms manage synchronous, independent state | |
; ref-set must be called within a transaction | |
; alter does the same thing as ref-set but alter implies the new | |
; value is a function of the old. ref-set implies old value is | |
; being obliterated and the ref will point to a new value. | |
; If a Transaction is initiated inside another transaction | |
; the inner transaction becomes part of the outer and commits | |
; when the outer does | |
(def a-ref (ref '[a b c d])) | |
(dosync (ref-set a-ref (rest @a-ref))) | |
(defn loop-thru [n] | |
(if-not (empty? n) | |
(do (prn (first n)) | |
(recur (rest n))))) | |
(loop-thru @a-ref) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment