-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch.cljc
304 lines (246 loc) · 9.54 KB
/
ch.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
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
;; Copyright (c) Konrad Grzanek
;; Created 2016-10-05
;; Re-designed 2018-12-21
(ns cljc.kongra.ch
(:require
[clojure.set
:refer [intersection difference]]
[clojure.string
:refer [blank?]]
#?(:clj [clojure.spec.alpha
:as spec])
#?(:cljs [cljs.pprint
:refer [pprint]])
#?(:cljs [cljs.kongra.ch.macros
:refer-macros [chP chReg chC chSpec]])))
#?(:clj (set! *warn-on-reflection* true))
;; ERROR REPORTING
(defn errMessage
[x]
#?(:clj (with-out-str
(print "ch(eck) failed on\n") (pr x)
(print " of type ") (pr (class x)))
:cljs (str "ch(eck) failed on\n" (with-out-str (pprint x))
"of type " (with-out-str (pprint (type x)))
"and typeof " (.typeOf js/goog x))))
;; REGISTRY OF COMMON CHECKS
(def checksRegistry (atom {}))
(declare chBool chSeq)
(defn asPred
[check x]
(chBool
(try
(check x) true
#?(:clj (catch AssertionError _ false))
#?(:clj (catch ClassCastException _ false))
#?(:clj (catch NullPointerException _ false))
#?(:clj (catch clojure.lang.ExceptionInfo _ false))
#?(:cljs (catch js/Error _ false)))))
(defn chs
[x]
(chSeq
(->> @checksRegistry
(map (fn [[name check]] (when (asPred check x) name)))
(filter some?)
sort)))
(defn chsAll
[& xs]
(chSeq
(->> xs
(map #(set (chs %)))
(reduce intersection)
sort)))
(defn chsDiff
[& xs]
(chSeq
(->> xs
(map #(set (chs %)))
(reduce difference)
sort)))
;; CLJ MACROS
#?(:clj (defmacro chP
[expr]
(let [x (symbol "x")]
`(fn [~x] (assert ~expr (errMessage ~x)) ~x))))
#?(:clj (defmacro chSpec
[spec]
(let [x (symbol "x")]
`(fn [~x]
(spec/assert ~spec ~x)))))
#?(:clj (defmacro chC
[expr]
(let [x (symbol "x")]
`(fn
([check#]
(fn [~x]
(assert ~expr (errMessage ~x))
(doseq [e# ~x] (check# e#))
~x))
([check# ~x]
(assert ~expr (errMessage ~x))
(doseq [e# ~x] (check# e#))
~x)))))
#?(:clj (defmacro chReg
([check]
(let [name (str check)]
`(let [name# (str ~name)]
(assert (fn? ~check))
(swap! checksRegistry
(fn [m#]
(when (m# name#)
(println "WARNING: (ch)eck name already in use:" name#))
(assoc m# name# ~check)))
nil)))
([name check]
`(swap! checksRegistry assoc ~name ~check))))
#?(:clj (defmacro defchP
[name expr]
(let [name (vary-meta name assoc
:arglists `'([~(symbol "x")])
:style/indent [0])]
`(def ~name (chP ~expr)))))
#?(:clj (defmacro defchC
[name expr]
(let [name (vary-meta name assoc
:arglists `'([~(symbol "x")]
[~(symbol "check") ~(symbol "x")])
:style/indent [0])]
`(def ~name (chC ~expr)))))
;; COMMON CLOJURE AND CLOJURE-SCRIPT CHECKS
(defn chOptional [check x] (if (nil? x) x (check x)))
(def chIdent (fn [x] x))
(def chUnit (chP (nil? x)))
(def chSome (chP (some? x)))
(def chAtom
(chP (instance?
#?(:clj clojure.lang.Atom
:cljs cljs.core/Atom) x))) (chReg chAtom)
(def chAtomOf
(chC (instance?
#?(:clj clojure.lang.Atom
:cljs cljs.core/Atom) x)))
(def chAssoc (chP (associative? x))) (chReg chAssoc)
(def chBool (chP (boolean? x))) (chReg chBool)
(def chChar (chP (char? x))) (chReg chChar)
(def chColl (chP (coll? x))) (chReg chColl)
(def chCounted (chP (counted? x))) (chReg chCounted)
(def chDelay (chP (delay? x))) (chReg chDelay)
(def chFn (chP (fn? x))) (chReg chFn)
(def chIfn (chP (ifn? x))) (chReg chIfn)
(def chIndexed (chP (indexed? x))) (chReg chIndexed)
(def chInteger (chP (integer? x))) (chReg chInteger)
(def chKeyword (chP (keyword? x))) (chReg chKeyword)
(def chList (chP (list? x))) (chReg chList)
(def chMap (chP (map? x))) (chReg chMap)
(def chNonEmpty (chP (seq x))) (chReg chNonEmpty)
(def chRecord (chP (record? x))) (chReg chRecord)
(def chReduced (chP (reduced? x))) (chReg chReduced)
(def chReversible (chP (reversible? x))) (chReg chReversible)
(def chSeq (chP (seq? x))) (chReg chSeq)
(def chSeqable (chP (seqable? x))) (chReg chSeqable)
(def chSequential (chP (sequential? x))) (chReg chSequential)
(def chSet (chP (set? x))) (chReg chSet)
(def chSorted (chP (sorted? x))) (chReg chSorted)
(def chString (chP (string? x))) (chReg chString)
(def chSymbol (chP (symbol? x))) (chReg chSymbol)
(def chVar (chP (var? x))) (chReg chVar)
(def chVector (chP (vector? x))) (chReg chVector)
(def chAssocOf (chC (associative? x)))
(def chCountedOf (chC (counted? x)))
(def chListOf (chC (list? x)))
(def chNonEmptyOf (chC (seq x)))
(def chReversibleOf (chC (reversible? x)))
(def chSetOf (chC (set? x)))
(def chSortedOf (chC (sorted? x)))
(def chVectorOf (chC (vector? x)))
(def chNonBlank
(chP #?(:clj (not (blank? x))
:cljs (and (string? x)
(not (.isEmptyOrWhitespace js/goog.string x))))))
(chReg chNonBlank)
;; COMMON CLOJURE AND CLOJURE-SCRIPT NUMBERS CH(ECK)S
(def chPosInt (chP (and (int? x) (pos? x)))) (chReg chPosInt)
(def chNegInt (chP (and (int? x) (neg? x)))) (chReg chNegInt)
(def chNatInt (chP (and (int? x) (>= x 0)))) (chReg chNatInt)
(def chEvenInt (chP (and (int? x) (even? x)))) (chReg chEvenInt)
(def chOddInt (chP (and (int? x) (odd? x)))) (chReg chOddInt)
(def chFloat (chP (float? x))) (chReg chFloat)
(def chInt (chP (int? x))) (chReg chInt)
(def chNumber (chP (number? x))) (chReg chNumber)
;; CLOJURE CHECKS
#?(:clj (defchP chAgent (instance? clojure.lang.Agent x)))
#?(:clj (chReg chAgent))
#?(:clj (defchP chASeq (instance? clojure.lang.ASeq x)))
#?(:clj (chReg chASeq))
#?(:clj (defchP chDeref (instance? clojure.lang.IDeref x)))
#?(:clj (chReg chDeref))
#?(:clj (defchP chLazy (instance? clojure.lang.LazySeq x)))
#?(:clj (chReg chLazy))
#?(:clj (defchP chLookup (instance? clojure.lang.ILookup x)))
#?(:clj (chReg chLookup))
#?(:clj (defchP chRef (instance? clojure.lang.Ref x)))
#?(:clj (chReg chRef))
#?(:clj (def chFuture (chP (future? x))))
#?(:clj (chReg chFuture))
#?(:clj (def chClass (chP (class? x))))
#?(:clj (chReg chClass))
;; CLOJURE NUMBER CHECKS
#?(:clj (def chRational (chP (rational? x))))
#?(:clj (chReg chRational))
#?(:clj (def chRatio (chP (ratio? x))))
#?(:clj (chReg chRatio))
#?(:clj (def chDecimal (chP (decimal? x))))
#?(:clj (chReg chDecimal))
#?(:clj (defn chLong ^long [^long x] x))
#?(:clj (chReg chLong))
#?(:clj (defn chNatLong ^long
[^long x]
(assert (>= x 0) (errMessage x))
x))
#?(:clj (chReg chNatLong))
#?(:clj (defn chPosLong ^long
[^long x]
(assert (> x 0) (errMessage x))
x))
#?(:clj (chReg chPosLong))
#?(:clj (defn chLongIn ^long
[^long m ^long n ^long x]
(assert (<= m n))
(assert (<= m x) (errMessage x))
(assert (<= x n) (errMessage x))
x))
#?(:clj (defn chDouble ^double [^double x] x))
#?(:clj (chReg chDouble))
#?(:clj (defn chDoubleIn ^double
[^double a ^double b ^double x]
(assert (<= a b))
(assert (<= a x) (errMessage x))
(assert (<= x b) (errMessage x))
x))
#?(:clj (defn chPosDouble ^double
[^double x]
(assert (> x 0) (errMessage x))
x))
#?(:clj (chReg chPosDouble))
#?(:clj (defn chNegDouble ^double
[^double x]
(assert (< x 0) (errMessage x))
x))
#?(:clj (chReg chNegDouble))
#?(:clj (defn ch0+Double ^double
[^double x]
(assert (>= x 0) (errMessage x))
x))
#?(:clj (chReg ch0+Double))
;; CLOJURE-SCRIPT CHECKS
#?(:cljs (def chObject (chP (object? x)))) #?(:cljs (chReg chObject))
#?(:cljs (def chArray (chP (array? x)))) #?(:cljs (chReg chArray))
#?(:cljs (def chDouble (chP (double? x)))) #?(:cljs (chReg chDouble))
#?(:cljs (def chPosDouble (chP (and (double? x) (pos? x))))) #?(:cljs (chReg chPosDouble))
#?(:cljs (def chNegDouble (chP (and (double? x) (neg? x))))) #?(:cljs (chReg chNegDouble))
#?(:cljs (def ch0+Double (chP (and (double? x) (>= x 0))))) #?(:cljs (chReg ch0+Double))
;; JAVA CHECKS
(def chJavaColl (chP (instance? java.util.Collection x))) (chReg chJavaColl)
(def chJavaList (chP (instance? java.util.List x))) (chReg chJavaList)
(def chJavaMap (chP (instance? java.util.Map x))) (chReg chJavaMap)
(def chJavaSet (chP (instance? java.util.Set x))) (chReg chJavaSet)