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 (meditations
"In the beginning, there was a word" "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" "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." "You can be the change you wish to see in the world."
(= __ (do (= "better"
(dosync (ref-set the-world "better")) (do (dosync (ref-set the-world "better"))
@the-world)) @the-world))
"Alter where you need not replace" "Alter where you need not replace"
(= __ (let [exclamator (fn [x] (str x "!"))] (= "better!!!"
(dosync (let [exclamator (fn [x] (str x "!"))]
(alter the-world exclamator)
(alter the-world exclamator) (dosync (alter the-world exclamator)
(alter the-world exclamator)) (alter the-world exclamator)
@the-world)) (alter the-world exclamator))
@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 (dosync (ref-set the-world 0))
@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 + 20))))
"Two worlds are better than one" "Two worlds are better than one"
(= ["Real Jerry" "Bizarro Jerry"] (= ["Real Jerry" "Bizarro Jerry"]
@ -39,4 +41,4 @@
(ref-set the-world {}) (ref-set the-world {})
(alter the-world assoc :jerry "Real Jerry") (alter the-world assoc :jerry "Real Jerry")
(alter bizarro-world assoc :jerry "Bizarro Jerry") (alter bizarro-world assoc :jerry "Bizarro Jerry")
__)))) print(map :jerry [@the-world @bizarro-world])))))