/
core.clj
357 lines (286 loc) · 7.58 KB
/
core.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
;; ## Stateful Arrays
;;
;; ova provides an array structure that allows for
;; state manipulation and watches over the entire
;; array as well as
(ns ova.core
(:require [clojure.string :as s]
[clojure.set :as set]
[hara.common.error :refer [suppress]]
[hara.common.fn :refer [get-> pcheck-> check-> suppress-pcheck]]
[hara.state :refer [hash-keyword]]))
(defprotocol OvaProtocol
(empty! [ova])
(get-ref [ova])
(clear-watches [ova])
(add-elem-watch [ova k f])
(remove-elem-watch [ova k])
(get-elem-watches [ova])
(clear-elem-watches [ova])
(get-filtered [ova k sel nv]))
(defn- ova-state []
{::data (ref [])
::watches (atom {})})
(defn- make-iwatch [ova]
(fn [k & args]
(doseq [w (get-elem-watches ova)]
(let [wk (first w)
wf (second w)]
(apply wf wk ova args)))))
(defn- add-iwatch [ova irf]
(let [k (hash-keyword ova)
f (make-iwatch ova)]
(add-watch irf k f)))
(defn- remove-iwatch [ova irf]
(let [k (hash-keyword ova)]
(remove-watch irf k)))
(deftype Ova [state]
OvaProtocol
(empty! [ova]
(for [rf @ova]
(remove-iwatch ova rf))
(ref-set (::data state) [])
ova)
(get-ref [ova]
(::data state))
(clear-watches [ova]
(doseq [[k _] (.getWatches ova)]
(remove-watch ova k)))
(add-elem-watch [ova k f]
(swap! (::watches state) assoc k f))
(remove-elem-watch [ova k]
(swap! (::watches state) dissoc k))
(get-elem-watches [ova]
(deref (::watches state)))
(clear-elem-watches [ova]
(reset! (::watches state) {}))
(get-filtered [ova k sel nv]
(cond (and (nil? sel) (integer? k))
(nth ova k nv)
:else
(let [res (->> (map deref @ova)
(filter (fn [m] (check-> m (or sel :id) k)))
first)]
(or res nv))))
clojure.lang.IDeref
(deref [ova] @(::data state))
clojure.lang.IRef
(setValidator [ova vf]
(.setValidator (::data state) vf))
(getValidator [ova]
(.getValidator (::data state)))
(getWatches [ova]
(.getWatches (::data state)))
(addWatch [ova key callback]
(add-watch (::data state) key callback))
(removeWatch [ova key]
(remove-watch (::data state) key))
clojure.lang.ITransientCollection
(conj [ova v]
(let [ev (ref v)]
(add-iwatch ova ev)
(alter (::data state) conj ev))
ova)
(persistent [ova]
(mapv deref @(::data state)))
clojure.lang.ITransientAssociative
(assoc [ova k v]
(if-let [pv (get @ova k)]
(ref-set pv v)
(let [ev (ref v)]
(add-iwatch ova ev)
(alter (::data state) assoc k ev)))
ova)
clojure.lang.ITransientVector
(assocN [ova i v] (assoc ova i v))
(pop [ova]
(if-let [lv (last @ova)]
(remove-iwatch ova lv))
(alter (::data state) pop)
ova)
clojure.lang.ILookup
(valAt [ova k]
(get-filtered ova k nil nil))
(valAt [ova k not-found]
(get-filtered ova k nil not-found))
clojure.lang.Indexed
(nth [ova i]
(nth ova i nil))
(nth [ova i not-found]
(if-let [entry (nth @ova i)]
@entry not-found))
clojure.lang.Counted
(count [ova] (count @ova))
clojure.lang.Seqable
(seq [ova]
(let [res (map deref (seq @ova))]
(if-not (empty? res) res)))
clojure.lang.IFn
(invoke [ova k] (get ova k))
(invoke [ova sel k] (get-filtered ova k sel nil))
java.lang.Object
(toString [ova]
(str (persistent! ova))))
(defmethod print-method
Ova
[ova w]
(print-method
(let [hash (.hashCode ova)
contents (->> @ova
(mapv #(-> % deref)))]
(format "<Ova@%s %s>"
hash contents)) w))
(defn concat! [ova es]
(doseq [e es] (conj! ova e))
ova)
(defn append! [ova & es] (concat! ova es))
(defn refresh!
([ova coll]
(empty! ova)
(concat! ova coll)))
(defn reinit!
([ova]
(empty! ova)
(clear-watches ova)
(clear-elem-watches ova)
ova)
([ova coll]
(reinit! ova)
(concat! ova coll)
ova))
(defn ova
([] (Ova. (ova-state)))
([coll]
(let [ova (Ova. (ova-state))]
(dosync (concat! ova coll))
ova)))
(defn- make-elem-change-watch [sel f]
(fn [k ov rf p n]
(let [pv (get-> p sel)
nv (get-> n sel)]
(cond (and (nil? pv) (nil? nv))
nil
(or (nil? pv) (nil? nv)
(not (= pv nv)))
(f k ov rf pv nv)))))
(defn add-elem-change-watch [ov k sel f]
(add-elem-watch ov k (make-elem-change-watch sel f)))
(defn indices
([ova] (-> (count ova) range vec))
([ova pchk]
(cond
(number? pchk)
(if (suppress (get ova pchk)) (list pchk) ())
(set? pchk)
(mapcat #(indices ova %) pchk)
:else
(filter (comp not nil?)
(map-indexed (fn [i obj]
(suppress-pcheck obj pchk i))
ova)))))
(defn selectv
([ova]
(persistent! ova))
([ova pchk]
(cond (number? pchk)
(if-let [val (suppress (get ova pchk))]
(list val) ())
(set? pchk) (mapcat #(selectv ova %) pchk)
:else (filter
(fn [obj] (suppress-pcheck obj pchk obj))
ova))))
(defn select
([ova] (set (selectv ova)))
([ova pchk]
(set (selectv ova pchk))))
(defn has? [ova pchk]
(-> (select ova pchk) empty? not))
(defn map! [ova f & args]
(doseq [evm @ova]
(apply alter evm f args))
ova)
(defn map-indexed! [ova f]
(doseq [i (range (count ova))]
(alter (@ova i) #(f i %) ))
ova)
(defn smap! [ova pchk f & args]
(let [idx (indices ova pchk)]
(doseq [i idx]
(apply alter (@ova i) f args)))
ova)
(defn smap-indexed! [ova pchk f]
(let [idx (indices ova pchk)]
(doseq [i idx]
(alter (@ova i) #(f i %))))
ova)
(defn insert-fn [v val & [i]]
(if (nil? i)
(conj v val)
(vec (clojure.core/concat (conj (subvec v 0 i) val)
(subvec v i)))))
(defn insert! [ova val & [i]]
(let [evm (ref val)]
(add-iwatch ova evm)
(alter (get-ref ova) insert-fn evm i))
ova)
(defn sort!
([ova] (sort! ova compare))
([ova comp]
(alter (get-ref ova)
#(sort (fn [x y]
(comp @x @y)) %))
ova)
([ova sel comp]
(alter (get-ref ova)
#(sort (fn [x y]
(comp (get-> @x sel) (get-> @y sel))) %))
ova))
(defn reverse! [ova]
(alter (get-ref ova) reverse)
ova)
(defn- delete-iwatches [ova idx]
(map-indexed (fn [i obj]
(if-not (idx i)
obj
(do (remove-iwatch ova obj) obj)))
@ova)
ova)
(defn- delete-iobjs [ova indices]
(->> ova
(map-indexed (fn [i obj] (if-not (indices i) obj)))
(filter (comp not nil?))
vec))
(defn delete-indices [ova idx]
(delete-iwatches ova idx)
(alter (get-ref ova) delete-iobjs idx)
ova)
(defn remove! [ova pchk]
(let [idx (set (indices ova pchk))]
(delete-indices ova idx))
ova)
(defn filter! [ova pchk]
(let [idx (set/difference
(set (range (count ova)))
(set (indices ova pchk)))]
(delete-indices ova idx))
ova)
(defn !! [ova pchk val]
(smap! ova pchk (constantly val)))
(defmacro << [& forms]
`(let [out# (dosync ~@forms)]
(persistent! out#)))
(defmacro !>
[ova pchk & forms]
`(smap! ~ova ~pchk
#(-> % ~@forms)))
(comment
(<< (def ov (ova [1 2 3 4 5]))
(append! ov 6 7 8 9))
(def ov (ova [{}]))
(dosync (!> ov 0
(assoc-in [:a :b] 1)
(update-in [:a :b] inc)
(assoc :c 3))
)
(<< (!> ov 0 assoc :d 1))
)