-
Notifications
You must be signed in to change notification settings - Fork 4
/
core.cljc
423 lines (329 loc) · 9.17 KB
/
core.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
(ns kezban.core
(:require [clojure.pprint :as pp]
[clojure.walk :as walk]
[clojure.string :as str]
#?(:clj [clojure.java.io :as io]))
#?(:cljs (:require-macros [cljs.core :as core]
[cljs.support :refer [assert-args]]))
#?(:clj (:import
(java.io StringWriter)
(java.util.concurrent TimeUnit)
(java.net URL)
(clojure.lang RT))))
#?(:cljs
(defmacro assert-all
"Internal - do not use!"
[fnname & pairs]
`(do (when-not ~(first pairs)
(throw (ex-info ~(str fnname " requires " (second pairs)) {:clojure.error/phase :macro-syntax-check})))
~(let [more (nnext pairs)]
(when more
(list* `assert-all fnname more))))))
#?(:clj
(defmacro assert-all
[& pairs]
`(do (when-not ~(first pairs)
(throw (IllegalArgumentException.
(str (first ~'&form) " requires " ~(second pairs) " in " ~'*ns* ":" (:line (meta ~'&form))))))
~(let [more (nnext pairs)]
(when more
(list* `assert-all more))))))
(defmacro def-
[name x]
(list `def (with-meta name (assoc (meta name) :private true)) x))
(defmacro when-let*
"Multiple binding version of when-let"
[bindings & body]
(when (seq bindings)
(assert-all #?(:cljs when-let*)
(vector? bindings) "a vector for its binding"
(even? (count bindings)) "exactly even forms in binding vector"))
(if (seq bindings)
`(when-let [~(first bindings) ~(second bindings)]
(when-let* ~(vec (drop 2 bindings)) ~@body))
`(do ~@body)))
(defmacro if-let*
"Multiple binding version of if-let"
([bindings then]
`(if-let* ~bindings ~then nil))
([bindings then else]
(when (seq bindings)
(assert-all #?(:cljs if-let*)
(vector? bindings) "a vector for its binding"
(even? (count bindings)) "exactly even forms in binding vector"))
(if (seq bindings)
`(if-let [~(first bindings) ~(second bindings)]
(if-let* ~(vec (drop 2 bindings)) ~then ~else)
~else)
then)))
(defmacro cond-as->
[expr name & clauses]
(assert-all #?(:cljs cond-as->)
(even? (count clauses)) "exactly even forms in clauses")
(let [g (gensym)
steps (map (fn [[test step]] `(if ~test ~step ~name))
(partition 2 clauses))]
`(let [~g ~expr
~name ~g
~@(interleave (repeat name) (butlast steps))]
~(if (empty? steps)
name
(last steps)))))
(defmacro ->>>
"Takes a set of functions and value at the end of the arguments.
Returns a result that is the composition
of those funtions.Applies the rightmost of fns to the args(last arg is the value/input!),
the next fn (left-to-right) to the result, etc."
[& form]
`((comp ~@(reverse (rest form))) ~(first form)))
(defn drop-first
"Return a lazy sequence of all, except first value"
[coll]
(drop 1 coll))
(defn nth-safe
"Returns the value at the index. get returns nil if index out of
bounds,unlike nth nth-safe does not throw an exception, returns nil instead.nth-safe
also works for strings, Java arrays, regex Matchers and Lists, and,
in O(n) time, for sequences."
([coll n]
(nth-safe coll n nil))
([coll n not-found]
(nth coll n not-found)))
(defn nnth
[coll index & indices]
(reduce #(nth-safe %1 %2) coll (cons index indices)))
(defn third
"Gets the third element from collection"
[coll]
(nth-safe coll 2))
(defn fourth
"Gets the fourth element from collection"
[coll]
(nth-safe coll 3))
(defn fifth
"Gets the fifth element from collection"
[coll]
(nth-safe coll 4))
(defn sixth
"Gets the sixth element from collection"
[coll]
(nth-safe coll 5))
(defn seventh
"Gets the seventh element from collection"
[coll]
(nth-safe coll 6))
(defn eighth
"Gets the eighth element from collection"
[coll]
(nth-safe coll 7))
(defn ninth
"Gets the ninth element from collection"
[coll]
(nth-safe coll 8))
(defn tenth
"Gets the tenth element from collection"
[coll]
(nth-safe coll 9))
(defn- xor-result
[x y]
(if (and x y)
false
(or x y)))
#?(:clj
(defmacro xor
([] true)
([x] x)
([x & next]
(let [first x
second `(first '(~@next))
;; used this eval approach because of lack of private function usage in macro!
result (xor-result (eval first) (eval second))]
`(if (= (count '~next) 1)
~result
(xor ~result ~@(rest next)))))))
#?(:clj
(defmacro pprint-macro
[form]
`(pp/pprint (walk/macroexpand-all '~form))))
#?(:clj
(defn eval-when
[test form]
(when test (eval form))))
;;does not support syntax-quote
(defmacro quoted?
[form]
(if (coll? form)
(let [f (first form)
s (str f)]
`(= "quote" ~s))
false))
(defn- type->str
[type]
(case type
:char "class [C"
:byte "class [B"
:short "class [S"
:int "class [I"
:long "class [J"
:float "class [F"
:double "class [D"
:boolean "class [Z"
nil))
(defn array?
([arr]
(array? nil arr))
([type arr]
(let [c (class arr)]
(if type
(and (= (type->str type) (str c)) (.isArray c))
(or (some-> c .isArray) false)))))
(defmacro error?
"Returns true if executing body throws an error(exception, error etc.), false otherwise."
[& body]
`(try
~@body
false
(catch #?(:clj Throwable) #?(:cljs js/Error) _#
true)))
(defn lazy?
[x]
(= "class clojure.lang.LazySeq" (str (type x))))
(defn any?
[pred coll]
((complement not-any?) pred coll))
(defn any-pred
[& preds]
(complement (apply every-pred (map complement preds))))
(defmacro try->
[x & forms]
`(try
(-> ~x ~@forms)
(catch #?(:clj Throwable) #?(:cljs js/Error) _#)))
(defmacro try->>
[x & forms]
`(try
(->> ~x ~@forms)
(catch #?(:clj Throwable) #?(:cljs js/Error) _#)))
(defn has-ns?
[ns]
(boolean (try-> ns find-ns)))
(defn multi-comp
([fns a b]
(multi-comp fns < a b))
([[f & others :as fns] order a b]
(if (seq fns)
(let [result (compare (f a) (f b))
f-result (if (= order >) (* -1 result) result)]
(if (= 0 f-result)
(recur others order a b)
f-result))
0)))
#?(:clj
(defmacro with-out-str-data-map
[& body]
`(let [s# (StringWriter.)]
(binding [*out* s#]
(let [r# ~@body]
{:result r#
:str (str s#)})))))
#?(:clj
(defmacro with-err-str
[& body]
`(let [s# (StringWriter.)]
(binding [*err* s#]
(eval '(do ~@body))
(str s#)))))
(defmacro letm
[bindings]
(assert-all #?(:cljs letm)
(vector? bindings) "a vector for its binding"
(even? (count bindings)) "an even number of forms in binding vector")
`(let* ~(destructure bindings)
(merge ~@(map #(hash-map (keyword %) %) (take-nth 2 bindings)))))
(defn in?
[x coll]
(boolean (some #(= x %) coll)))
(defn take-while-and-n-more
[pred n coll]
(let [[head tail] (split-with pred coll)]
(concat head (take n tail))))
(defn dissoc-in
([m ks]
(if-let [[k & ks] (seq ks)]
(if (seq ks)
(let [v (dissoc-in (get m k) ks)]
(if (empty? v)
(dissoc m k)
(assoc m k v)))
(dissoc m k))
m))
([m ks & kss]
(if-let [[ks' & kss] (seq kss)]
(recur (dissoc-in m ks) ks' kss)
(dissoc-in m ks))))
#?(:clj
(defmacro with-timeout
[msec & body]
`(.get (future ~@body) ~msec TimeUnit/MILLISECONDS)))
#?(:clj
(defn url->file
[src f]
(let [source (URL. src)]
(with-open [i (.openStream source)
o (io/output-stream f)]
(io/copy i o))
f)))
(defmacro cond-let
[bindings & forms]
(assert-all #?(:cljs cond-let)
(vector? bindings) "a vector for its binding"
(even? (count bindings)) "an even number of forms in binding vector")
`(let* ~(destructure bindings)
(cond ~@forms)))
(defn keywordize
[s]
(-> s
str/lower-case
str/trim
(str/replace #"\s+" "-")
keyword))
#?(:clj
(defn source-clj-file
[ns]
(require ns)
(some->> ns
ns-publics
vals
first
meta
:file
(.getResourceAsStream (RT/baseLoader))
slurp)))
#?(:clj
(defmacro time*
[& forms]
`(let [start# (. System (nanoTime))
ret# (do ~@forms)]
{:duration (/ (double (- (. System (nanoTime)) start#)) 1000000.0)
:result ret#})))
(defn process-lazy-seq
[f threshold lazy-s]
(when-let [data (seq (take threshold lazy-s))]
(doseq [d data]
(f d))
(recur f threshold (drop threshold lazy-s))))
(defmacro forall
[seq-exprs body-expr]
`(doall (for ~seq-exprs ~body-expr)))
#?(:clj
(defmacro when-no-aot
[& body]
`(when-not *compile-files*
~@body)))
#?(:clj
(defmacro defay
[name & forms]
`(do
(def ~name (delay ~@forms))
(when-not *compile-files*
(deref ~name)))))