forked from functional-koans/clojure-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17_atoms.clj
33 lines (26 loc) · 822 Bytes
/
17_atoms.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
(ns koans.17-atoms
(:require [koan-engine.core :refer :all]))
(def atomic-clock (atom 0))
(meditations
"Atoms are like refs"
(= 0 @atomic-clock)
"You can change at the swap meet"
(= 1 (do
(swap! atomic-clock inc)
@atomic-clock))
"Keep taxes out of this: swapping requires no transaction"
(= 5 (do
(swap! atomic-clock + 4)
@atomic-clock))
"Any number of arguments might happen during a swap"
(= 20 (do
(swap! atomic-clock + 1 2 3 4 5)
@atomic-clock))
"Atomic atoms are atomic"
(= 20 (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 20 :fin)
@atomic-clock)))