-
Notifications
You must be signed in to change notification settings - Fork 4
/
core.clj
240 lines (206 loc) · 5.36 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
(ns kezban.core
(:require [clojure.pprint :as pp]
[clojure.walk :as walk]))
(defn- kezban
[]
"I'M DAMN SEXY!")
(defmacro ^:private 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 when-let*
"Multiple binding version of when-let"
[bindings & body]
(when (seq bindings)
(assert-all
(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
(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 ->>>
"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)))
(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))))))
(defmacro pprint-macro
[form]
`(pp/pprint (walk/macroexpand-all '~form)))
(defn eval-when
[test form]
(when test (eval form)))
(defn has-ns?
[ns]
(try
(the-ns ns)
true
(catch Exception _
false)))
;;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 Throwable _#
true)))
(defn lazy?
[x]
(= "class clojure.lang.LazySeq" (str (type x))))
(defn any?
[pred coll]
((complement not-any?) pred coll))
(defn ?
[x]
(if x true false))
(defn any-pred
[& preds]
(complement (apply every-pred (map complement preds))))
(defmacro try->
[x & forms]
`(try
(-> ~x ~@forms)
(catch Throwable _#)))
(defmacro try->>
[x & forms]
`(try
(->> ~x ~@forms)
(catch Throwable _#)))
(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)))
(defmacro with-out-str-data-map
[& body]
`(let [s# (new java.io.StringWriter)]
(binding [*out* s#]
(let [r# ~@body]
{:result r#
:str (str s#)}))))
(defmacro letm
[bindings]
(assert-all
(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)))))