Add atoms.

This commit is contained in:
Colin Jones 2011-03-31 07:44:54 -05:00
parent 0c286f1686
commit 717207339f
4 changed files with 40 additions and 1 deletions

View File

@ -1,7 +1,6 @@
Concepts / Language Features Concepts / Language Features
===== =====
Agents Agents
Atoms
Vars Vars
state identity lifetime state identity lifetime
lazy sequences lazy sequences

31
src/koans/atoms.clj Normal file
View File

@ -0,0 +1,31 @@
(def atomic-clock (atom 0))
(meditations
"Atoms are like refs"
(= __ @atomic-clock)
"You can change at the swap meet"
(= __ (do
(swap! atomic-clock inc)
@atomic-clock))
"Keep taxes out of this: swapping requires no transaction"
(= 5 (do
__
@atomic-clock))
"Any number of arguments might happen during a swap"
(= __ (do
(swap! atomic-clock + 1 2 3 4 5)
@atomic-clock))
"Atomic atoms are atomic"
(= __ (do
(compare-and-set! atomic-clock 100 :fin)
@atomic-clock))
"When your expectations are aligned with reality things, proceed that way"
(= :fin (do
(compare-and-set! __ __ __)
@atomic-clock))
)

View File

@ -144,6 +144,14 @@
] ]
"___" ['(fn [x] (+ 20 x))]} "___" ['(fn [x] (+ 20 x))]}
"atoms" {"__" [0
1
'(swap! atomic-clock (partial + 4))
20
20
'atomic-clock 20 :fin
]}
"macros" {"__" ['~(first form) "macros" {"__" ['~(first form)
'~(nth form 2) '~(nth form 2)
'form 'form

View File

@ -34,6 +34,7 @@
"recursion" "recursion"
"destructuring" "destructuring"
"refs" "refs"
"atoms"
"macros" "macros"
"datatypes"]) "datatypes"])