-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
core.clj
4121 lines (3533 loc) · 136 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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns clojure.core)
(def unquote)
(def unquote-splicing)
(def
#^{:arglists '([& items])
:doc "Creates a new list containing the items."}
list (. clojure.lang.PersistentList creator))
(def
#^{:arglists '([x seq])
:doc "Returns a new seq where x is the first element and seq is
the rest."}
cons (fn* cons [x seq] (. clojure.lang.RT (cons x seq))))
;during bootstrap we don't have destructuring let, loop or fn, will redefine later
(def
#^{:macro true}
let (fn* let [& decl] (cons 'let* decl)))
(def
#^{:macro true}
loop (fn* loop [& decl] (cons 'loop* decl)))
(def
#^{:macro true}
fn (fn* fn [& decl] (cons 'fn* decl)))
(def
#^{:arglists '([coll])
:doc "Returns the first item in the collection. Calls seq on its
argument. If coll is nil, returns nil."}
first (fn first [coll] (. clojure.lang.RT (first coll))))
(def
#^{:arglists '([coll])
:tag clojure.lang.ISeq
:doc "Returns a seq of the items after the first. Calls seq on its
argument. If there are no more items, returns nil."}
next (fn next [x] (. clojure.lang.RT (next x))))
(def
#^{:arglists '([coll])
:tag clojure.lang.ISeq
:doc "Returns a possibly empty seq of the items after the first. Calls seq on its
argument."}
rest (fn rest [x] (. clojure.lang.RT (more x))))
(def
#^{:arglists '([coll x] [coll x & xs])
:doc "conj[oin]. Returns a new collection with the xs
'added'. (conj nil item) returns (item). The 'addition' may
happen at different 'places' depending on the concrete type."}
conj (fn conj
([coll x] (. clojure.lang.RT (conj coll x)))
([coll x & xs]
(if xs
(recur (conj coll x) (first xs) (next xs))
(conj coll x)))))
(def
#^{:doc "Same as (first (next x))"
:arglists '([x])}
second (fn second [x] (first (next x))))
(def
#^{:doc "Same as (first (first x))"
:arglists '([x])}
ffirst (fn ffirst [x] (first (first x))))
(def
#^{:doc "Same as (next (first x))"
:arglists '([x])}
nfirst (fn nfirst [x] (next (first x))))
(def
#^{:doc "Same as (first (next x))"
:arglists '([x])}
fnext (fn fnext [x] (first (next x))))
(def
#^{:doc "Same as (next (next x))"
:arglists '([x])}
nnext (fn nnext [x] (next (next x))))
(def
#^{:arglists '([coll])
:doc "Returns a seq on the collection. If the collection is
empty, returns nil. (seq nil) returns nil. seq also works on
Strings, native Java arrays (of reference types) and any objects
that implement Iterable."
:tag clojure.lang.ISeq}
seq (fn seq [coll] (. clojure.lang.RT (seq coll))))
(def
#^{:arglists '([#^Class c x])
:doc "Evaluates x and tests if it is an instance of the class
c. Returns true or false"}
instance? (fn instance? [#^Class c x] (. c (isInstance x))))
(def
#^{:arglists '([x])
:doc "Return true if x implements ISeq"}
seq? (fn seq? [x] (instance? clojure.lang.ISeq x)))
(def
#^{:arglists '([x])
:doc "Return true if x is a String"}
string? (fn string? [x] (instance? String x)))
(def
#^{:arglists '([x])
:doc "Return true if x implements IPersistentMap"}
map? (fn map? [x] (instance? clojure.lang.IPersistentMap x)))
(def
#^{:arglists '([x])
:doc "Return true if x implements IPersistentVector "}
vector? (fn vector? [x] (instance? clojure.lang.IPersistentVector x)))
(def
#^{:private true}
sigs
(fn [fdecl]
(if (seq? (first fdecl))
(loop [ret [] fdecl fdecl]
(if fdecl
(recur (conj ret (first (first fdecl))) (next fdecl))
(seq ret)))
(list (first fdecl)))))
(def
#^{:arglists '([map key val] [map key val & kvs])
:doc "assoc[iate]. When applied to a map, returns a new map of the
same (hashed/sorted) type, that contains the mapping of key(s) to
val(s). When applied to a vector, returns a new vector that
contains val at index. Note - index must be <= (count vector)."}
assoc
(fn assoc
([map key val] (. clojure.lang.RT (assoc map key val)))
([map key val & kvs]
(let [ret (assoc map key val)]
(if kvs
(recur ret (first kvs) (second kvs) (nnext kvs))
ret)))))
;;;;;;;;;;;;;;;;; metadata ;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def
#^{:arglists '([obj])
:doc "Returns the metadata of obj, returns nil if there is no metadata."}
meta (fn meta [x]
(if (instance? clojure.lang.IMeta x)
(. #^clojure.lang.IMeta x (meta)))))
(def
#^{:arglists '([#^clojure.lang.IObj obj m])
:doc "Returns an object of the same type and value as obj, with
map m as its metadata."}
with-meta (fn with-meta [#^clojure.lang.IObj x m]
(. x (withMeta m))))
(def
#^{:arglists '([coll])
:doc "Return the last item in coll, in linear time"}
last (fn last [s]
(if (next s)
(recur (next s))
(first s))))
(def
#^{:arglists '([coll])
:doc "Return a seq of all but the last item in coll, in linear time"}
butlast (fn butlast [s]
(loop [ret [] s s]
(if (next s)
(recur (conj ret (first s)) (next s))
(seq ret)))))
(def
#^{:doc "Same as (def name (fn [params* ] exprs*)) or (def
name (fn ([params* ] exprs*)+)) with any doc-string or attrs added
to the var metadata"
:arglists '([name doc-string? attr-map? [params*] body]
[name doc-string? attr-map? ([params*] body)+ attr-map?])}
defn (fn defn [name & fdecl]
(let [m (if (string? (first fdecl))
{:doc (first fdecl)}
{})
fdecl (if (string? (first fdecl))
(next fdecl)
fdecl)
m (if (map? (first fdecl))
(conj m (first fdecl))
m)
fdecl (if (map? (first fdecl))
(next fdecl)
fdecl)
fdecl (if (vector? (first fdecl))
(list fdecl)
fdecl)
m (if (map? (last fdecl))
(conj m (last fdecl))
m)
fdecl (if (map? (last fdecl))
(butlast fdecl)
fdecl)
m (conj {:arglists (list 'quote (sigs fdecl))} m)]
(list 'def (with-meta name (conj (if (meta name) (meta name) {}) m))
(cons `fn fdecl)))))
(. (var defn) (setMacro))
(defn cast
"Throws a ClassCastException if x is not a c, else returns x."
[#^Class c x]
(. c (cast x)))
(defn to-array
"Returns an array of Objects containing the contents of coll, which
can be any Collection. Maps to java.util.Collection.toArray()."
{:tag "[Ljava.lang.Object;"}
[coll] (. clojure.lang.RT (toArray coll)))
(defn vector
"Creates a new vector containing the args."
([] [])
([& args]
(. clojure.lang.LazilyPersistentVector (create args))))
(defn vec
"Creates a new vector containing the contents of coll."
([coll]
(. clojure.lang.LazilyPersistentVector (createOwning (to-array coll)))))
(defn hash-map
"keyval => key val
Returns a new hash map with supplied mappings."
([] {})
([& keyvals]
(. clojure.lang.PersistentHashMap (create keyvals))))
(defn hash-set
"Returns a new hash set with supplied keys."
([] #{})
([& keys]
(. clojure.lang.PersistentHashSet (create keys))))
(defn sorted-map
"keyval => key val
Returns a new sorted map with supplied mappings."
([& keyvals]
(. clojure.lang.PersistentTreeMap (create keyvals))))
(defn sorted-set
"Returns a new sorted set with supplied keys."
([& keys]
(. clojure.lang.PersistentTreeSet (create keys))))
(defn sorted-map-by
"keyval => key val
Returns a new sorted map with supplied mappings, using the supplied comparator."
([comparator & keyvals]
(. clojure.lang.PersistentTreeMap (create comparator keyvals))))
;;;;;;;;;;;;;;;;;;;;
(def
#^{:doc "Like defn, but the resulting function name is declared as a
macro and will be used as a macro by the compiler when it is
called."
:arglists '([name doc-string? attr-map? [params*] body]
[name doc-string? attr-map? ([params*] body)+ attr-map?])}
defmacro (fn [name & args]
(list 'do
(cons `defn (cons name args))
(list '. (list 'var name) '(setMacro))
(list 'var name))))
(. (var defmacro) (setMacro))
(defmacro when
"Evaluates test. If logical true, evaluates body in an implicit do."
[test & body]
(list 'if test (cons 'do body)))
(defmacro when-not
"Evaluates test. If logical false, evaluates body in an implicit do."
[test & body]
(list 'if test nil (cons 'do body)))
(defn nil?
"Returns true if x is nil, false otherwise."
{:tag Boolean}
[x] (identical? x nil))
(defn false?
"Returns true if x is the value false, false otherwise."
{:tag Boolean}
[x] (identical? x false))
(defn true?
"Returns true if x is the value true, false otherwise."
{:tag Boolean}
[x] (identical? x true))
(defn not
"Returns true if x is logical false, false otherwise."
{:tag Boolean}
[x] (if x false true))
(defn str
"With no args, returns the empty string. With one arg x, returns
x.toString(). (str nil) returns the empty string. With more than
one arg, returns the concatenation of the str values of the args."
{:tag String}
([] "")
([#^Object x]
(if (nil? x) "" (. x (toString))))
([x & ys]
((fn [#^StringBuilder sb more]
(if more
(recur (. sb (append (str (first more)))) (next more))
(str sb)))
(new StringBuilder #^String (str x)) ys)))
(defn symbol?
"Return true if x is a Symbol"
[x] (instance? clojure.lang.Symbol x))
(defn keyword?
"Return true if x is a Keyword"
[x] (instance? clojure.lang.Keyword x))
(defn symbol
"Returns a Symbol with the given namespace and name."
([name] (if (symbol? name) name (. clojure.lang.Symbol (intern name))))
([ns name] (. clojure.lang.Symbol (intern ns name))))
(defn keyword
"Returns a Keyword with the given namespace and name. Do not use :
in the keyword strings, it will be added automatically."
([name] (if (keyword? name) name (. clojure.lang.Keyword (intern nil name))))
([ns name] (. clojure.lang.Keyword (intern ns name))))
(defn gensym
"Returns a new symbol with a unique name. If a prefix string is
supplied, the name is prefix# where # is some unique number. If
prefix is not supplied, the prefix is 'G__'."
([] (gensym "G__"))
([prefix-string] (. clojure.lang.Symbol (intern (str prefix-string (str (. clojure.lang.RT (nextID))))))))
(defmacro cond
"Takes a set of test/expr pairs. It evaluates each test one at a
time. If a test returns logical true, cond evaluates and returns
the value of the corresponding expr and doesn't evaluate any of the
other tests or exprs. (cond) returns nil."
[& clauses]
(when clauses
(list 'if (first clauses)
(if (next clauses)
(second clauses)
(throw (IllegalArgumentException.
"cond requires an even number of forms")))
(cons 'clojure.core/cond (next (next clauses))))))
(defn spread
{:private true}
[arglist]
(cond
(nil? arglist) nil
(nil? (next arglist)) (seq (first arglist))
:else (cons (first arglist) (spread (next arglist)))))
(defn apply
"Applies fn f to the argument list formed by prepending args to argseq."
{:arglists '([f args* argseq])}
[#^clojure.lang.IFn f & args]
(. f (applyTo (spread args))))
(defn vary-meta
"Returns an object of the same type and value as obj, with
(apply f (meta obj) args) as its metadata."
[obj f & args]
(with-meta obj (apply f (meta obj) args)))
(defn list*
"Creates a new list containing the item prepended to more."
[item & more]
(spread (cons item more)))
(defmacro lazy-seq
"Takes a body of expressions that returns an ISeq or nil, and yields
a Seqable object that will invoke the body only the first time seq
is called, and will cache the result and return it on all subsequent
seq calls."
[& body]
(list 'new 'clojure.lang.LazySeq (list* '#^{:once true} fn* [] body)))
(defn concat
"Returns a lazy seq representing the concatenation of the elements in the supplied colls."
([] (lazy-seq nil))
([x] (lazy-seq x))
([x y]
(lazy-seq
(let [s (seq x)]
(if s
(cons (first s) (concat (rest s) y))
y))))
([x y & zs]
(let [cat (fn cat [xys zs]
(lazy-seq
(let [xys (seq xys)]
(if xys
(cons (first xys) (cat (rest xys) zs))
(when zs
(cat (first zs) (next zs)))))))]
(cat (concat x y) zs))))
;;;;;;;;;;;;;;;;at this point all the support for syntax-quote exists;;;;;;;;;;;;;;;;;;;;;;
(defmacro delay
"Takes a body of expressions and yields a Delay object that will
invoke the body only the first time it is forced (with force), and
will cache the result and return it on all subsequent force
calls."
[& body]
(list 'new 'clojure.lang.Delay (list* `#^{:once true} fn* [] body)))
(defn delay?
"returns true if x is a Delay created with delay"
[x] (instance? clojure.lang.Delay x))
(defn force
"If x is a Delay, returns the (possibly cached) value of its expression, else returns x"
[x] (. clojure.lang.Delay (force x)))
(defmacro if-not
"Evaluates test. If logical false, evaluates and returns then expr, otherwise else expr, if supplied, else nil."
([test then] `(if-not ~test ~then nil))
([test then else]
`(if (not ~test) ~then ~else)))
(defn =
"Equality. Returns true if x equals y, false if not. Same as
Java x.equals(y) except it also works for nil, and compares
numbers and collections in a type-independent manner. Clojure's immutable data
structures define equals() (and thus =) as a value, not an identity,
comparison."
{:tag Boolean
:inline (fn [x y] `(. clojure.lang.Util equiv ~x ~y))
:inline-arities #{2}}
([x] true)
([x y] (clojure.lang.Util/equiv x y))
([x y & more]
(if (= x y)
(if (next more)
(recur y (first more) (next more))
(= y (first more)))
false)))
(defn not=
"Same as (not (= obj1 obj2))"
{:tag Boolean}
([x] false)
([x y] (not (= x y)))
([x y & more]
(not (apply = x y more))))
(defn compare
"Comparator. Returns 0 if x equals y, -1 if x is logically 'less
than' y, else 1. Same as Java x.compareTo(y) except it also works
for nil, and compares numbers and collections in a type-independent
manner. x must implement Comparable"
{:tag Integer
:inline (fn [x y] `(. clojure.lang.Util compare ~x ~y))}
[x y] (. clojure.lang.Util (compare x y)))
(defmacro and
"Evaluates exprs one at a time, from left to right. If a form
returns logical false (nil or false), and returns that value and
doesn't evaluate any of the other expressions, otherwise it returns
the value of the last expr. (and) returns true."
([] true)
([x] x)
([x & next]
`(let [and# ~x]
(if and# (and ~@next) and#))))
(defmacro or
"Evaluates exprs one at a time, from left to right. If a form
returns a logical true value, or returns that value and doesn't
evaluate any of the other expressions, otherwise it returns the
value of the last expression. (or) returns nil."
([] nil)
([x] x)
([x & next]
`(let [or# ~x]
(if or# or# (or ~@next)))))
;;;;;;;;;;;;;;;;;;; sequence fns ;;;;;;;;;;;;;;;;;;;;;;;
(defn reduce
"f should be a function of 2 arguments. If val is not supplied,
returns the result of applying f to the first 2 items in coll, then
applying f to that result and the 3rd item, etc. If coll contains no
items, f must accept no arguments as well, and reduce returns the
result of calling f with no arguments. If coll has only 1 item, it
is returned and f is not called. If val is supplied, returns the
result of applying f to val and the first item in coll, then
applying f to that result and the 2nd item, etc. If coll contains no
items, returns val and f is not called."
([f coll]
(let [s (seq coll)]
(if s
(if (instance? clojure.lang.IReduce s)
(. #^clojure.lang.IReduce s (reduce f))
(reduce f (first s) (next s)))
(f))))
([f val coll]
(let [s (seq coll)]
(if (instance? clojure.lang.IReduce s)
(. #^clojure.lang.IReduce s (reduce f val))
((fn [f val s]
(if s
(recur f (f val (first s)) (next s))
val))
f val s)))))
(defn reverse
"Returns a seq of the items in coll in reverse order. Not lazy."
[coll]
(reduce conj () coll))
;;math stuff
(defn +
"Returns the sum of nums. (+) returns 0."
{:inline (fn [x y] `(. clojure.lang.Numbers (add ~x ~y)))
:inline-arities #{2}}
([] 0)
([x] (cast Number x))
([x y] (. clojure.lang.Numbers (add x y)))
([x y & more]
(reduce + (+ x y) more)))
(defn *
"Returns the product of nums. (*) returns 1."
{:inline (fn [x y] `(. clojure.lang.Numbers (multiply ~x ~y)))
:inline-arities #{2}}
([] 1)
([x] (cast Number x))
([x y] (. clojure.lang.Numbers (multiply x y)))
([x y & more]
(reduce * (* x y) more)))
(defn /
"If no denominators are supplied, returns 1/numerator,
else returns numerator divided by all of the denominators."
{:inline (fn [x y] `(. clojure.lang.Numbers (divide ~x ~y)))
:inline-arities #{2}}
([x] (/ 1 x))
([x y] (. clojure.lang.Numbers (divide x y)))
([x y & more]
(reduce / (/ x y) more)))
(defn -
"If no ys are supplied, returns the negation of x, else subtracts
the ys from x and returns the result."
{:inline (fn [& args] `(. clojure.lang.Numbers (minus ~@args)))
:inline-arities #{1 2}}
([x] (. clojure.lang.Numbers (minus x)))
([x y] (. clojure.lang.Numbers (minus x y)))
([x y & more]
(reduce - (- x y) more)))
(defn <
"Returns non-nil if nums are in monotonically increasing order,
otherwise false."
{:inline (fn [x y] `(. clojure.lang.Numbers (lt ~x ~y)))
:inline-arities #{2}}
([x] true)
([x y] (. clojure.lang.Numbers (lt x y)))
([x y & more]
(if (< x y)
(if (next more)
(recur y (first more) (next more))
(< y (first more)))
false)))
(defn <=
"Returns non-nil if nums are in monotonically non-decreasing order,
otherwise false."
{:inline (fn [x y] `(. clojure.lang.Numbers (lte ~x ~y)))
:inline-arities #{2}}
([x] true)
([x y] (. clojure.lang.Numbers (lte x y)))
([x y & more]
(if (<= x y)
(if (next more)
(recur y (first more) (next more))
(<= y (first more)))
false)))
(defn >
"Returns non-nil if nums are in monotonically decreasing order,
otherwise false."
{:inline (fn [x y] `(. clojure.lang.Numbers (gt ~x ~y)))
:inline-arities #{2}}
([x] true)
([x y] (. clojure.lang.Numbers (gt x y)))
([x y & more]
(if (> x y)
(if (next more)
(recur y (first more) (next more))
(> y (first more)))
false)))
(defn >=
"Returns non-nil if nums are in monotonically non-increasing order,
otherwise false."
{:inline (fn [x y] `(. clojure.lang.Numbers (gte ~x ~y)))
:inline-arities #{2}}
([x] true)
([x y] (. clojure.lang.Numbers (gte x y)))
([x y & more]
(if (>= x y)
(if (next more)
(recur y (first more) (next more))
(>= y (first more)))
false)))
(defn ==
"Returns non-nil if nums all have the same value, otherwise false"
{:inline (fn [x y] `(. clojure.lang.Numbers (equiv ~x ~y)))
:inline-arities #{2}}
([x] true)
([x y] (. clojure.lang.Numbers (equiv x y)))
([x y & more]
(if (== x y)
(if (next more)
(recur y (first more) (next more))
(== y (first more)))
false)))
(defn max
"Returns the greatest of the nums."
([x] x)
([x y] (if (> x y) x y))
([x y & more]
(reduce max (max x y) more)))
(defn min
"Returns the least of the nums."
([x] x)
([x y] (if (< x y) x y))
([x y & more]
(reduce min (min x y) more)))
(defn inc
"Returns a number one greater than num."
{:inline (fn [x] `(. clojure.lang.Numbers (inc ~x)))}
[x] (. clojure.lang.Numbers (inc x)))
(defn dec
"Returns a number one less than num."
{:inline (fn [x] `(. clojure.lang.Numbers (dec ~x)))}
[x] (. clojure.lang.Numbers (dec x)))
(defn unchecked-inc
"Returns a number one greater than x, an int or long.
Note - uses a primitive operator subject to overflow."
{:inline (fn [x] `(. clojure.lang.Numbers (unchecked_inc ~x)))}
[x] (. clojure.lang.Numbers (unchecked_inc x)))
(defn unchecked-dec
"Returns a number one less than x, an int or long.
Note - uses a primitive operator subject to overflow."
{:inline (fn [x] `(. clojure.lang.Numbers (unchecked_dec ~x)))}
[x] (. clojure.lang.Numbers (unchecked_dec x)))
(defn unchecked-negate
"Returns the negation of x, an int or long.
Note - uses a primitive operator subject to overflow."
{:inline (fn [x] `(. clojure.lang.Numbers (unchecked_negate ~x)))}
[x] (. clojure.lang.Numbers (unchecked_negate x)))
(defn unchecked-add
"Returns the sum of x and y, both int or long.
Note - uses a primitive operator subject to overflow."
{:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_add ~x ~y)))}
[x y] (. clojure.lang.Numbers (unchecked_add x y)))
(defn unchecked-subtract
"Returns the difference of x and y, both int or long.
Note - uses a primitive operator subject to overflow."
{:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_subtract ~x ~y)))}
[x y] (. clojure.lang.Numbers (unchecked_subtract x y)))
(defn unchecked-multiply
"Returns the product of x and y, both int or long.
Note - uses a primitive operator subject to overflow."
{:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_multiply ~x ~y)))}
[x y] (. clojure.lang.Numbers (unchecked_multiply x y)))
(defn unchecked-divide
"Returns the division of x by y, both int or long.
Note - uses a primitive operator subject to truncation."
{:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_divide ~x ~y)))}
[x y] (. clojure.lang.Numbers (unchecked_divide x y)))
(defn unchecked-remainder
"Returns the remainder of division of x by y, both int or long.
Note - uses a primitive operator subject to truncation."
{:inline (fn [x y] `(. clojure.lang.Numbers (unchecked_remainder ~x ~y)))}
[x y] (. clojure.lang.Numbers (unchecked_remainder x y)))
(defn pos?
"Returns true if num is greater than zero, else false"
{:tag Boolean
:inline (fn [x] `(. clojure.lang.Numbers (isPos ~x)))}
[x] (. clojure.lang.Numbers (isPos x)))
(defn neg?
"Returns true if num is less than zero, else false"
{:tag Boolean
:inline (fn [x] `(. clojure.lang.Numbers (isNeg ~x)))}
[x] (. clojure.lang.Numbers (isNeg x)))
(defn zero?
"Returns true if num is zero, else false"
{:tag Boolean
:inline (fn [x] `(. clojure.lang.Numbers (isZero ~x)))}
[x] (. clojure.lang.Numbers (isZero x)))
(defn quot
"quot[ient] of dividing numerator by denominator."
[num div]
(. clojure.lang.Numbers (quotient num div)))
(defn rem
"remainder of dividing numerator by denominator."
[num div]
(. clojure.lang.Numbers (remainder num div)))
(defn rationalize
"returns the rational value of num"
[num]
(. clojure.lang.Numbers (rationalize num)))
;;Bit ops
(defn bit-not
"Bitwise complement"
{:inline (fn [x] `(. clojure.lang.Numbers (not ~x)))}
[x] (. clojure.lang.Numbers not x))
(defn bit-and
"Bitwise and"
{:inline (fn [x y] `(. clojure.lang.Numbers (and ~x ~y)))}
[x y] (. clojure.lang.Numbers and x y))
(defn bit-or
"Bitwise or"
{:inline (fn [x y] `(. clojure.lang.Numbers (or ~x ~y)))}
[x y] (. clojure.lang.Numbers or x y))
(defn bit-xor
"Bitwise exclusive or"
{:inline (fn [x y] `(. clojure.lang.Numbers (xor ~x ~y)))}
[x y] (. clojure.lang.Numbers xor x y))
(defn bit-and-not
"Bitwise and with complement"
[x y] (. clojure.lang.Numbers andNot x y))
(defn bit-clear
"Clear bit at index n"
[x n] (. clojure.lang.Numbers clearBit x n))
(defn bit-set
"Set bit at index n"
[x n] (. clojure.lang.Numbers setBit x n))
(defn bit-flip
"Flip bit at index n"
[x n] (. clojure.lang.Numbers flipBit x n))
(defn bit-test
"Test bit at index n"
[x n] (. clojure.lang.Numbers testBit x n))
(defn bit-shift-left
"Bitwise shift left"
[x n] (. clojure.lang.Numbers shiftLeft x n))
(defn bit-shift-right
"Bitwise shift right"
[x n] (. clojure.lang.Numbers shiftRight x n))
(defn even?
"Returns true if n is even, throws an exception if n is not an integer"
[n] (zero? (bit-and n 1)))
(defn odd?
"Returns true if n is odd, throws an exception if n is not an integer"
[n] (not (even? n)))
;;
(defn complement
"Takes a fn f and returns a fn that takes the same arguments as f,
has the same effects, if any, and returns the opposite truth value."
[f]
(fn
([] (not (f)))
([x] (not (f x)))
([x y] (not (f x y)))
([x y & zs] (not (apply f x y zs)))))
(defn constantly
"Returns a function that takes any number of arguments and returns x."
[x] (fn [& args] x))
(defn identity
"Returns its argument."
[x] x)
;;Collection stuff
(defn count
"Returns the number of items in the collection. (count nil) returns
0. Also works on strings, arrays, and Java Collections and Maps"
[coll] (. clojure.lang.RT (count coll)))
;;list stuff
(defn peek
"For a list or queue, same as first, for a vector, same as, but much
more efficient than, last. If the collection is empty, returns nil."
[coll] (. clojure.lang.RT (peek coll)))
(defn pop
"For a list or queue, returns a new list/queue without the first
item, for a vector, returns a new vector without the last item. If
the collection is empty, throws an exception. Note - not the same
as next/butlast."
[coll] (. clojure.lang.RT (pop coll)))
(defn nth
"Returns the value at the index. get returns nil if index out of
bounds, nth throws an exception unless not-found is supplied. nth
also works for strings, Java arrays, regex Matchers and Lists, and,
in O(n) time, for sequences."
([coll index] (. clojure.lang.RT (nth coll index)))
([coll index not-found] (. clojure.lang.RT (nth coll index not-found))))
;;map stuff
(defn contains?
"Returns true if key is present in the given collection, otherwise
returns false. Note that for numerically indexed collections like
vectors and Java arrays, this tests if the numeric key is within the
range of indexes. 'contains?' operates constant or logarithmic time;
it will not perform a linear search for a value. See also 'some'."
[coll key] (. clojure.lang.RT (contains coll key)))
(defn get
"Returns the value mapped to key, not-found or nil if key not present."
([map key]
(. clojure.lang.RT (get map key)))
([map key not-found]
(. clojure.lang.RT (get map key not-found))))
(defn dissoc
"dissoc[iate]. Returns a new map of the same (hashed/sorted) type,
that does not contain a mapping for key(s)."
([map] map)
([map key]
(. clojure.lang.RT (dissoc map key)))
([map key & ks]
(let [ret (dissoc map key)]
(if ks
(recur ret (first ks) (next ks))
ret))))
(defn disj
"disj[oin]. Returns a new set of the same (hashed/sorted) type, that
does not contain key(s)."
([set] set)
([#^clojure.lang.IPersistentSet set key]
(. set (disjoin key)))
([set key & ks]
(let [ret (disj set key)]
(if ks
(recur ret (first ks) (next ks))
ret))))
(defn find
"Returns the map entry for key, or nil if key not present."
[map key] (. clojure.lang.RT (find map key)))
(defn select-keys
"Returns a map containing only those entries in map whose key is in keys"
[map keyseq]
(loop [ret {} keys (seq keyseq)]
(if keys
(let [entry (. clojure.lang.RT (find map (first keys)))]
(recur
(if entry
(conj ret entry)
ret)
(next keys)))
ret)))
(defn keys
"Returns a sequence of the map's keys."
[map] (. clojure.lang.RT (keys map)))
(defn vals
"Returns a sequence of the map's values."
[map] (. clojure.lang.RT (vals map)))
(defn key
"Returns the key of the map entry."
[#^java.util.Map$Entry e]
(. e (getKey)))
(defn val
"Returns the value in the map entry."
[#^java.util.Map$Entry e]
(. e (getValue)))
(defn rseq
"Returns, in constant time, a seq of the items in rev (which
can be a vector or sorted-map), in reverse order. If rev is empty returns nil"
[#^clojure.lang.Reversible rev]
(. rev (rseq)))
(defn name
"Returns the name String of a symbol or keyword."
{:tag String}
[#^clojure.lang.Named x]
(. x (getName)))
(defn namespace
"Returns the namespace String of a symbol or keyword, or nil if not present."
{:tag String}
[#^clojure.lang.Named x]
(. x (getNamespace)))
(defmacro locking
"Executes exprs in an implicit do, while holding the monitor of x.
Will release the monitor of x in all circumstances."
[x & body]
`(let [lockee# ~x]
(try
(monitor-enter lockee#)
~@body
(finally
(monitor-exit lockee#)))))
(defmacro ..
"form => fieldName-symbol or (instanceMethodName-symbol args*)
Expands into a member access (.) of the first member on the first
argument, followed by the next member on the result, etc. For
instance:
(.. System (getProperties) (get \"os.name\"))
expands to:
(. (. System (getProperties)) (get \"os.name\"))
but is easier to write, read, and understand."
([x form] `(. ~x ~form))
([x form & more] `(.. (. ~x ~form) ~@more)))
(defmacro ->
"Threads the expr through the forms. Inserts x as the
second item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the