-
Notifications
You must be signed in to change notification settings - Fork 2
/
refs.cljc
53 lines (46 loc) · 1.21 KB
/
refs.cljc
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
43
44
45
46
47
48
49
50
51
52
53
(ns com.wsscode.misc.refs
#?(:clj
(:import
(clojure.lang
Atom
IDeref
Volatile))))
(defn kw-identical?
"Clojure/Clojurescript efficient keyword comparison."
[kw-a kw-b]
#?(:clj (identical? kw-a kw-b)
:cljs (keyword-identical? kw-a kw-b)))
(defn atom?
"Check if x is an atom."
[x]
#?(:clj (instance? IDeref x)
:cljs (satisfies? IDeref x)))
(defprotocol GenericBox
(-greset! [this value])
(-gswap!
[this f]
[this f x]
[this f x y]
[this f x y more]))
(extend-protocol GenericBox
Atom
(-greset! [this value] (reset! this value))
(-gswap!
([this f] (swap! this f))
([this f x] (swap! this f x))
([this f x y] (swap! this f x y))
([this f x y more] (apply swap! this f x y more)))
Volatile
(-greset! [this value] (vreset! this value))
(-gswap!
([this f] (vswap! this f))
([this f x] (vswap! this f x))
([this f x y] (vswap! this f x y))
([this f x y more] (vreset! this (apply f @this x y more)))))
(defn greset!
[this val] (-greset! this val))
(defn gswap!
([this f] (-gswap! this f))
([this f x] (-gswap! this f x))
([this f x y] (-gswap! this f x y))
([this f x y & more] (-gswap! this f x y more)))