-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16_refs.clj
42 lines (34 loc) · 1.23 KB
/
16_refs.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(ns koans.16-refs
(:require [koan-engine.core :refer :all]))
(def the-world (ref "hello"))
(def bizarro-world (ref {}))
(meditations
"In the beginning, there was a word"
(= "hello" (deref the-world))
"You can get the word more succinctly, but it's the same"
(= "hello" @the-world)
"You can be the change you wish to see in the world."
(= "better" (do
(dosync (ref-set the-world "better"))
@the-world))
"Alter where you need not replace"
(= "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 (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 #(+ 20 %)))))
"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")
(vec (map #(:jerry %) (list @the-world @bizarro-world)))))))