Finished koan 16

This commit is contained in:
Matt Davidson 2022-11-24 20:22:08 +00:00
parent 40755feb94
commit 12a5b7aaec

View file

@ -6,31 +6,33 @@
(meditations
"In the beginning, there was a word"
(= __ (deref the-world))
(= "hello" (deref the-world))
"You can get the word more succinctly, but it's the same"
(= __ @the-world)
(= "hello" @the-world)
"You can be the change you wish to see in the world."
(= __ (do
(dosync (ref-set the-world "better"))
(= "better"
(do (dosync (ref-set the-world "better"))
@the-world))
"Alter where you need not replace"
(= __ (let [exclamator (fn [x] (str x "!"))]
(dosync
(alter the-world exclamator)
(= "better!!!"
(let [exclamator (fn [x] (str x "!"))]
(dosync (alter the-world exclamator)
(alter the-world exclamator)
(alter the-world exclamator))
@the-world))
"Don't forget to do your work in a transaction!"
(= 0 (do __
(= 0 (do (dosync (ref-set the-world 0))
@the-world))
"Functions passed to alter may depend on the data in the ref"
(= 20 (do
(dosync (alter the-world ___))))
(dosync (alter the-world + 20))))
"Two worlds are better than one"
(= ["Real Jerry" "Bizarro Jerry"]
@ -39,4 +41,4 @@
(ref-set the-world {})
(alter the-world assoc :jerry "Real Jerry")
(alter bizarro-world assoc :jerry "Bizarro Jerry")
__))))
print(map :jerry [@the-world @bizarro-world])))))