Provide example of coordinated ref update.

This commit is contained in:
Colin Jones 2011-03-31 07:24:09 -05:00
parent 8a21ad89bd
commit 0c286f1686
2 changed files with 14 additions and 9 deletions

View File

@ -1,4 +1,5 @@
(def the-world (ref "hello")) (def the-world (ref "hello"))
(def bizarro-world (ref {}))
(meditations (meditations
"In the beginning, there was a word" "In the beginning, there was a word"
@ -20,16 +21,20 @@
(alter the-world exclamator)) (alter the-world exclamator))
@the-world)) @the-world))
"Though you should keep transactions as short as possible"
(= "better!!!!!!" (letfn [(exclamator [x] (str x "!"))]
(dosync
(alter the-world ___))
@the-world))
"Don't forget to do your work in a transaction!" "Don't forget to do your work in a transaction!"
(= 0 (do __ (= 0 (do __
@the-world)) @the-world))
"Functions passed to alter may depend on the data in the ref" "Functions passed to alter may depend on the data in the ref"
(= 20 (do (= 20 (do
(dosync (alter the-world ___))))) (dosync (alter the-world ___))))
"Two worlds are better than one"
(= ["Real Jerry" "Bizarro Jerry"]
(do
(dosync
(ref-set the-world {})
(alter the-world assoc :jerry "Real Jerry")
(alter bizarro-world assoc :jerry "Bizarro Jerry")
__))))

View File

@ -140,9 +140,9 @@
"better" "better"
"better!!!" "better!!!"
'(dosync (ref-set the-world 0)) '(dosync (ref-set the-world 0))
'(map :jerry [@the-world @bizarro-world])
] ]
"___" ['(comp exclamator exclamator exclamator) "___" ['(fn [x] (+ 20 x))]}
'(fn [x] (+ 20 x))]}
"macros" {"__" ['~(first form) "macros" {"__" ['~(first form)
'~(nth form 2) '~(nth form 2)