/
las3r.core.lsr
2553 lines (2093 loc) · 74.9 KB
/
las3r.core.lsr
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.
;; Copyright (c) Aemon Cannon. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
;; which can be found in the file CPL.TXT 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.
(in-ns 'las3r.core)
(def *compiler* (. *runtime* compiler))
(def
#^{:arglists '([& items])
:doc "Flash trace."}
trace (. com.las3r.runtime.RT (objectForName "trace")))
(def
#^{:arglists '([& items])
:doc "Creates a new list containing the items."}
list (. com.las3r.runtime.List 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] (. com.las3r.runtime.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}
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] (. com.las3r.runtime.RT (first coll))))
(def
#^{:arglists '([coll])
:doc "Returns a seq of the items after the first. Calls seq on its
argument. If there are no more items, returns nil."}
rest (fn rest [x] (. com.las3r.runtime.RT (rest x))))
(def
#^{:doc "Same as (first (rest x))"
:arglists '([x])}
second (fn second [x] (first (rest x))))
(def
#^{:doc "Same as (first (first x))"
:arglists '([x])}
ffirst (fn ffirst [x] (first (first x))))
(def
#^{:doc "Same as (rest (first x))"
:arglists '([x])}
rfirst (fn rfirst [x] (rest (first x))))
(def
#^{:doc "Same as (first (rest x))"
:arglists '([x])}
frest (fn frest [x] (first (rest x))))
(def
#^{:doc "Same as (rest (rest x))"
:arglists '([x])}
rrest (fn rrest [x] (rest (rest 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] (. com.las3r.runtime.RT (conj coll x)))
([coll x & xs]
(if xs
(recur (conj coll x) (first xs) (rest xs))
(conj coll x)))))
(def
#^{:arglists '([coll])
:doc "Sequence. Returns a new ISeq on the collection. If the
collection is empty, returns nil. (seq nil) returns nil. seq also
works on Strings and native arrays."
:tag com.las3r.runtime.ISeq}
seq (fn seq [coll] (. com.las3r.runtime.RT (seq coll))))
(def
#^{:arglists '([c x])
:doc "Evaluates x and tests if it is an instance of the class
c. Returns true or false"}
instance? (fn instance? [c x] (. com.las3r.runtime.RT (isInstance c x))))
(def
#^{:arglists '([x])
:doc "Return true if x implements ISeq"}
seq? (fn seq? [x] (instance? com.las3r.runtime.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 IMap"}
map? (fn map? [x] (instance? com.las3r.runtime.IMap x)))
(def
#^{:arglists '([x])
:doc "Return true if x implements IVector "}
vector? (fn vector? [x] (instance? com.las3r.runtime.IVector x)))
(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] (. com.las3r.runtime.RT (assoc map key val)))
([map key val & kvs]
(let [ret (assoc map key val)]
(if kvs
(recur ret (first kvs) (second kvs) (rrest kvs))
ret)))))
;;;;;;;;;;;;;;;; loop ;;;;;;;;;;;;;;;;;;;;;;;;;
(def
#^{:macro true
:doc "Loop macro that expands into a let expression with a body that
applies a function to the bound locals."}
loop* (fn* loop* [bindings & body]
(let [names-vals (. com.las3r.runtime.RT (unzip (seq bindings)))
names (. com.las3r.runtime.PersistentVector (createFromSeq (seq (first names-vals))))
vals (seq (second names-vals))]
(concat (list 'let bindings (concat (list (concat (list 'fn names) body)) names)))))
)
;;;;;;;;;;;;;;;;; metadata ;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def
#^{:arglists '([obj])
:doc "Returns the metadata of obj, returns nil if there is no metadata."}
meta (fn meta [x]
(if (instance? com.las3r.runtime.IObj x)
(. #^com.las3r.runtime.IObj x meta))))
(def
#^{:arglists '([#^com.las3r.runtime.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 [#^com.las3r.runtime.IObj x m]
(. x (withMeta m))))
(def
#^{:arglists '([coll])
:doc "Return the last item in coll, in linear time"}
last (fn last [s]
(if (rest s)
(recur (rest s))
(first s))))
(def
#^{:arglists '([coll])
:doc "Return a sequence of all but the last item in coll, in linear time"}
butlast (fn butlast [s]
(loop* [ret [] s s]
(if (rest s)
(recur (conj ret (first s)) (rest s))
(seq ret)))))
(def
#^{:private true}
sigs
(fn [fdecl]
(if (seq? (first fdecl))
(loop* [ret [] fdecl fdecl]
(if fdecl
(recur (conj ret (first (first fdecl))) (rest fdecl))
(seq ret)))
(list (first fdecl)))))
(def
#^{:doc "Same as (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))
(rest fdecl)
fdecl)
m (if (map? (first fdecl))
(conj m (first fdecl))
m)
fdecl (if (map? (first fdecl))
(rest 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)
]
`(def ~(with-meta name (conj (if (meta name) (meta name) {}) m)) (fn ~@fdecl)))))
(. (var defn) (setMacro))
(defn to-array
"Returns an array of Objects containing the contents of coll, which
can be any Collection."
[coll] (. com.las3r.runtime.RT (toArray coll)))
(def ary to-array)
(defn cast
"Throws an Error if x is not a c, else returns x."
[c x] (. com.las3r.runtime.RT (cast c x)))
(defn vector
"Creates a new vector containing the args."
[& args]
(. com.las3r.runtime.PersistentVector (createFromSeq args)))
(defn hash-map
"keyval => key val
Returns a new hash map with supplied mappings."
[& keyvals]
(. com.las3r.runtime.PersistentHashMap (createFromSeq keyvals)))
(defn array-map
"keyval => key val
Returns a new array map with supplied mappings."
[& keyvals]
(. com.las3r.runtime.PersistentArrayMap (createFromSeq keyvals)))
(defn hash-set
"Returns a new hash set with supplied values."
[& vals]
(. com.las3r.runtime.PersistentHashSet (createFromSeq vals)))
(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)))))
(. (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}
([] "")
([x]
(if (nil? x) "" (cast String x )))
([x & ys]
(loop* [sb (str x)
more ys]
(if more
(recur (. sb (concat (str (first more)))) (rest more))
(str sb)))))
(defn char-code->str
"Return the string corresponding to the numeric code."
[code]
(. String (fromCharCode code)))
(defn symbol
"Returns a Symbol with the given namespace and name."
([name] (. com.las3r.runtime.Symbol (intern1 *runtime* name)))
([ns name] (. com.las3r.runtime.Symbol (intern2 *runtime* 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] (. com.las3r.runtime.Keyword (intern2 *runtime* nil name)))
([ns name] (. com.las3r.runtime.Keyword (intern2 *runtime* 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] (. com.las3r.runtime.Symbol (intern1 *runtime* (str prefix-string (str (. *runtime* (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)
(second clauses)
(cons 'cond (rest (rest clauses))))))
(defn spread
{:private true}
[arglist]
(cond
(nil? arglist) nil
(nil? (rest arglist)) (seq (first arglist))
:else (cons (first arglist) (spread (rest arglist)))))
(defn apply
"Applies fn f to the argument list formed by prepending args to argseq."
{:arglists '([f args* argseq])}
[f & args]
(apply* f (spread args)))
(defn list*
"Creates a new list containing the item prepended to more."
[item & more]
(spread (cons item more)))
(defmacro lazy-cons
"Expands to code which produces a seq object whose first is
first-expr and whose rest is rest-expr, neither of which is
evaluated until first/rest is called. Each expr will be evaluated at most
once per step in the sequence, e.g. calling first/rest repeatedly on the
same node of the seq evaluates first/rest-expr once - the values they yield are
cached."
[first-expr & rest-expr]
(list 'new 'com.las3r.runtime.LazyCons (list `fn (list [] first-expr) (list* [(gensym)] rest-expr))))
(defn concat
"Returns a lazy seq representing the concatenation of the elements in the supplied colls."
([] nil)
([x] (seq x))
([x y]
(if (seq x)
(lazy-cons (first x) (concat (rest x) y))
(seq y)))
([x y & zs]
(let [cat (fn cat [xys zs]
(if (seq xys)
(lazy-cons (first xys) (cat (rest xys) zs))
(when zs
(recur (first zs) (rest zs)))))]
(cat (concat x y) zs))))
(defn throw-rte
"Throw a runtime error with the given message."
[m] (throw (new com.las3r.errors.RuntimeError m)))
;;;;;;;;;;;;;;;;at this point all the support for syntax-quote exists;;;;;;;;;;;;;;;;;;;;;;
(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 in a type-independent manner. Clojure's immutable data
structures define equals() (and thus =) as a value, not an identity,
comparison."
{:tag Boolean}
([x] true)
([x y] (. com.las3r.util.Util (equal x y)))
([x y & more]
(if (= x y)
(if (rest more)
(recur y (first more) (rest 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 in a type-independent manner. x must
implement Comparable"
{:tag Number
:inline (fn [x y] `(. com.las3r.util.Util compare ~x ~y))}
[x y] (. com.las3r.util.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 & rest]
`(let [and# ~x]
(if and# (and ~@rest) 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 & rest]
`(let [or# ~x]
(if or# or# (or ~@rest)))))
;;;;;;;;;;;;;;;;;;; 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
(reduce f (first s) (rest s))
(f))))
([f val coll]
(let [s (seq coll)]
(if (instance? com.las3r.runtime.IReduce s)
(. #^com.las3r.runtime.IReduce s (reduce f val))
((fn [f val s]
(if s
(recur f (f val (first s)) (rest s))
val))
f val s)))))
(defn reverse
"Returns a seq of the items in coll in reverse order. Not lazy."
[coll]
(reduce conj nil coll))
;; Special functions
(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 [& args]
(not (apply f args))))
(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)
(defn comp
"Takes a set of functions and returns a fn that is the composition
of those fns. The returned fn takes a variable number of args,
applies the rightmost of fns to the args, the next
fn (right-to-left) to the result, etc."
[& fs]
(let [fs (reverse fs)]
(fn [& args]
(loop* [ret (apply (first fs) args) fs (rest fs)]
(if fs
(recur ((first fs) ret) (rest fs))
ret)))))
(defn partial
"Takes a function f and fewer than the normal arguments to f, and
returns a fn that takes a variable number of additional args. When
called, the returned function calls f with args + additional args."
([f arg1]
(fn [& args] (apply f arg1 args)))
([f arg1 arg2]
(fn [& args] (apply f arg1 arg2 args)))
([f arg1 arg2 arg3]
(fn [& args] (apply f arg1 arg2 arg3 args)))
([f arg1 arg2 arg3 & more]
(fn [& args] (apply f arg1 arg2 arg3 (concat more args)))))
;;map stuff
(defn contains?
"Returns true if key is present, else false."
[map key] (. com.las3r.runtime.RT (contains map key)))
(defn get
"Returns the value mapped to key, not-found or nil if key not present."
([map key]
(. com.las3r.runtime.RT (get map key)))
([map key not-found]
(. com.las3r.runtime.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]
(. com.las3r.runtime.RT (dissoc map key)))
([map key & ks]
(let [ret (dissoc map key)]
(if ks
(recur ret (first ks) (rest ks))
ret))))
(defn disj
"disj[oin]. Returns a new set of the same (hashed/sorted) type, that
does not contain key(s)."
([set] set)
([#^com.las3r.runtime.ISet set key]
(. set (remove key)))
([set key & ks]
(let [ret (disj set key)]
(if ks
(recur ret (first ks) (rest ks))
ret))))
(defn find
"Returns the map entry for key, or nil if key not present."
[map key] (. com.las3r.runtime.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 (. com.las3r.runtime.RT (find map (first keys)))]
(recur
(if entry
(conj ret entry)
ret)
(rest keys)))
ret)))
(defn keys
"Returns a sequence of the map's keys."
[map] (. com.las3r.runtime.RT (keys map)))
(defn vals
"Returns a sequence of the map's values."
[map] (. com.las3r.runtime.RT (vals map)))
(defn key
"Returns the key of the map entry."
[#^com.las3r.runtime.MapEntry e]
(. e key))
(defn val
"Returns the value in the map entry."
[#^com.las3r.runtime.MapEntry e]
(. e value))
(defn name
"Returns the name String of a symbol or keyword."
[x]
(. x (getName)))
(defn namespace
"Returns the namespace String of a symbol or keyword, or nil if not present."
[x]
(. x (getNamespace)))
;;math stuff
(defn +
"Returns the sum of nums. (+) returns 0."
([] 0)
([x] (cast Number x))
([x y] (. com.las3r.runtime.Numbers (add x y)))
([x y & more]
(reduce + (+ x y) more)))
(defn *
"Returns the product of nums. (*) returns 1."
([] 1)
([x] (cast Number x))
([x y] (. com.las3r.runtime.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."
([x] (/ 1 x))
([x y] (. com.las3r.runtime.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."
([x] (. com.las3r.runtime.Numbers (negate x)))
([x y] (. com.las3r.runtime.Numbers (minus x y)))
([x y & more]
(reduce - (- x y) more)))
(defn <
"Returns non-nil if nums are in monotonically increasing order,
otherwise false."
([x] true)
([x y] (. com.las3r.runtime.Numbers (lt x y)))
([x y & more]
(if (< x y)
(if (rest more)
(recur y (first more) (rest more))
(< y (first more)))
false)))
(defn <=
"Returns non-nil if nums are in monotonically non-decreasing order,
otherwise false."
([x] true)
([x y] (. com.las3r.runtime.Numbers (lte x y)))
([x y & more]
(if (<= x y)
(if (rest more)
(recur y (first more) (rest more))
(<= y (first more)))
false)))
(defn >
"Returns non-nil if nums are in monotonically decreasing order,
otherwise false."
([x] true)
([x y] (. com.las3r.runtime.Numbers (gt x y)))
([x y & more]
(if (> x y)
(if (rest more)
(recur y (first more) (rest more))
(> y (first more)))
false)))
(defn >=
"Returns non-nil if nums are in monotonically non-increasing order,
otherwise false."
([x] true)
([x y] (. com.las3r.runtime.Numbers (gte x y)))
([x y & more]
(if (>= x y)
(if (rest more)
(recur y (first more) (rest more))
(>= y (first more)))
false)))
(defn ==
"Returns non-nil if nums all have the same value, otherwise false"
([x] true)
([x y] (. com.las3r.runtime.Numbers (equiv x y)))
([x y & more]
(if (== x y)
(if (rest more)
(recur y (first more) (rest 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."
[x] (. com.las3r.runtime.Numbers (inc x)))
(defn dec
"Returns a number one less than num."
[x] (. com.las3r.runtime.Numbers (dec x)))
(defn pos?
"Returns true if num is greater than zero, else false"
[x] (> x 0))
(defn neg?
"Returns true if num is less than zero, else false"
[x] (< x 0))
(defn zero?
"Returns true if num is zero, else false"
[x] (= x 0))
(defn rem
"rem[ainder] of dividing numerator by denominator."
[num div]
(. com.las3r.runtime.Numbers (remainder num div)))
;;Bit ops
(defn bit-not
"Bitwise complement"
[x] (. com.las3r.runtime.Numbers (not x)))
(defn bit-and
"Bitwise and"
([x] x)
([x y] (. com.las3r.runtime.Numbers (and x y)))
([x y & more]
(reduce bit-and (bit-and x y) more)))
(defn bit-or
"Bitwise or"
([x] x)
([x y] (. com.las3r.runtime.Numbers (or x y)))
([x y & more]
(reduce bit-or (bit-or x y) more)))
(defn bit-xor
"Bitwise exclusive or"
([x] x)
([x y] (. com.las3r.runtime.Numbers (xor x y)))
([x y & more]
(reduce bit-and (bit-and x y) more)))
(defn bit-shl
"Bitwise shift left"
([x n] (. com.las3r.runtime.Numbers (shl x n))))
(defn bit-shr
"Bitwise shift right"
([x n] (. com.las3r.runtime.Numbers (shr x n))))
(defn bit-sar
"Bitwise shift arithmetic right"
([x n] (. com.las3r.runtime.Numbers (sar x n))))
;; clojury aliases
(def bit-shift-left bit-shl)
(def bit-shift-right bit-sar)
(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)))
;;;;;;;;; var stuff
(defmacro binding
"binding => var-symbol init-expr
Creates new bindings for the (already-existing) vars, with the
supplied initial values, executes the exprs in an implicit do, then
re-establishes the bindings that existed before."
[bindings & body]
(let [var-ize (fn [var-vals]
(loop* [ret [] vvs (seq var-vals)]
(if vvs
(recur (conj (conj ret `(var ~(first vvs))) (second vvs))
(rest (rest vvs)))
(seq ret))))]
`(do
(. com.las3r.runtime.Var (pushBindings *runtime* (hash-map ~@(var-ize bindings))))
(try
~@body
(finally
(. com.las3r.runtime.Var (popBindings *runtime*)))))))
(defn find-var
"Returns the global var named by the namespace-qualified symbol, or
nil if no var with that name."
[sym] (. com.las3r.runtime.Var (find *runtime* sym)))
;; Collection stuff
(defn count
"Returns the number of items in the collection. (count nil) returns
0. Also works on strings, arrays and maps"
[coll] (. com.las3r.runtime.RT (count coll)))
;;list stuff
(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] (. com.las3r.runtime.RT (nth coll index)))
([coll index not-found] (. com.las3r.runtime.RT (nth coll index not-found))))
;;;;;;;;;;;;;;;;; Tracing to console...
(defn pr
"Trace out the object. By default, pr and prn print in a way that objects
can be read by the reader"
([] nil)
([x]
(. *runtime* (print x *out*))
nil)
([x & more]
(pr x)
(. *out* (write \space))
(apply pr more)))
(defn newline
"Writes a newline to the output stream that is the current value of
*out*"
[]
(. *out* (write \newline))
nil)
(defn prn
"Same as pr followed by (newline)."
[& more]
(apply pr more)
(newline))
(defn print
"Trace out the object(s). print and println produce output for human consumption."
[& more]
(binding [*print-readably* nil]
(apply pr more)))
(defn println
"Same as print followed by (newline)"
[& more]
(binding [*print-readably* nil]
(apply prn more)))
(defmacro assert
"Evaluates expr and throws an exception if it does not evaluate to
logical true."
[x]
`(when-not ~x
(throw (new Error (str "Assert failed: " '~x)))))
;;;;;;;;;;;;;;;;;;; sequence fns ;;;;;;;;;;;;;;;;;;;;;;;
(defn not-empty
"If coll is empty, returns nil, else coll"
[coll] (when (seq coll) coll))
(defn every?
"Returns true if (pred x) is logical true for every x in coll, else
false."
[pred coll]
(if (seq coll)
(and (pred (first coll))
(recur pred (rest coll)))
true))
(def
#^{:tag Boolean
:doc "Returns false if (pred x) is logical true for every x in
coll, else true."
:arglists '([pred coll])}
not-every? (comp not every?))
(defn some
"Returns the first logical true value of (pred x) for any x in coll,
else nil."
[pred coll]
(when (seq coll)
(or (pred (first coll)) (recur pred (rest coll)))))
(def
#^{:tag Boolean
:doc "Returns false if (pred x) is logical true for any x in coll,
else true."
:arglists '([pred coll])}
not-any? (comp not some))
(defn map
"Returns a lazy seq consisting of the result of applying f to the
set of first items of each coll, followed by applying f to the set
of second items in each coll, until any one of the colls is
exhausted. Any remaining items in other colls are ignored. Function
f should accept number-of-colls arguments."
([f coll]
(when (seq coll)
(lazy-cons (f (first coll)) (map f (rest coll)))))
([f coll & colls]
(when (and (seq coll) (every? seq colls))
(lazy-cons (apply f (first coll) (map first colls))
(apply map f (rest coll) (map rest colls))))))
(defn mapcat
"Returns the result of applying concat to the result of applying map
to f and colls. Thus function f should return a collection."
[f & colls]
(apply concat (apply map f colls)))
(defn filter
"Returns a lazy seq of the items in coll for which
(pred item) returns true. pred must be free of side-effects."
[pred coll]
(when (seq coll)