Let's look at the solutions for the next of Clojure Koans -Refs.
;refs
(def the-world (ref "hello"))
(def bizarro-world (ref {}))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;1
(= __ (deref the-world))
; got to be hello
(= "hello" (deref the-world))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;2
(= __ @the-world)
; got to be hello
(= "hello" @the-world)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;3
(= __ (do
(dosync (ref-set the-world "better"))
@the-world))
; you better work it out
(= "better" (do
(dosync (ref-set the-world "better"))
@the-world))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;4
(= __ (let [exclamator (fn [x] (str x "!"))]
(dosync
(alter the-world
exclamator)
(alter the-world
exclamator)
(alter the-world
exclamator))
@the-world))
(= "better!!!" (let [exclamator (fn [x] (str x "!"))]
(dosync
(alter the-world
exclamator)
(alter the-world
exclamator)
(alter the-world
exclamator))
@the-world))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;5
(= 0 (do __
@the-world))
(= 0 (do (dosync (ref-set the-world 0) )
@the-world))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;6
(= 20 (do
(dosync (alter the-world ___))))
(= 20 (do
(dosync (alter the-world #(+ % 20)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;7
(= ["Real Jerry" "Bizarro Jerry"]
(do
(dosync
(ref-set the-world {})
(alter the-world assoc :jerry "Real Jerry")
(alter bizarro-world
assoc :jerry "Bizarro Jerry")
__))))
(= ["Real Jerry" "Bizarro Jerry"]
(do
(dosync
(ref-set the-world {})
(alter the-world assoc :jerry "Real Jerry")
(alter bizarro-world
assoc :jerry "Bizarro Jerry"))
[(:jerry @the-world) (:jerry @bizarro-world)]))
No comments:
Post a Comment