clojure-koans/src/koans/15_refs.clj

43 lines
1.1 KiB
Clojure
Raw Normal View History

(ns koans.15-refs
(:require [koan-engine.core :refer :all]))
2010-11-10 02:19:10 +00:00
(def the-world (ref "hello"))
(def bizarro-world (ref {}))
2010-11-10 02:19:10 +00:00
(meditations
"In the beginning, there was a word"
(= __ (deref the-world))
"You can get the word more succinctly, but it's the same"
(= __ @the-world)
"You can be the change you wish to see in the world."
(= __ (do
(dosync (ref-set the-world "better"))
@the-world))
"Alter where you need not replace"
(= __ (let [exclamator (fn [x] (str x "!"))]
(dosync
2011-10-26 02:55:54 +00:00
(alter the-world exclamator)
(alter the-world exclamator)
(alter the-world exclamator))
2010-11-10 02:19:10 +00:00
@the-world))
"Don't forget to do your work in a transaction!"
(= 0 (do __
2011-10-26 02:55:54 +00:00
@the-world))
"Functions passed to alter may depend on the data in the ref"
(= 20 (do
(dosync (alter the-world ___))))
"Two worlds are better than one"
(= ["Real Jerry" "Bizarro Jerry"]
2011-10-26 02:55:54 +00:00
(do
(dosync
(ref-set the-world {})
(alter the-world assoc :jerry "Real Jerry")
(alter bizarro-world assoc :jerry "Bizarro Jerry")
__))))