Skip to content

Instantly share code, notes, and snippets.

@greghelton
Created December 27, 2011 00:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greghelton/1522324 to your computer and use it in GitHub Desktop.
Save greghelton/1522324 to your computer and use it in GitHub Desktop.
Clojure: use recur & use Clojure transactiopn
; 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