-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prelude.inc
1405 lines (1239 loc) · 45.7 KB
/
Prelude.inc
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.
;pure laziness to leave these here ...
(def splice-unquote)
(def finally)
(def newline (fn* newline [] (print "\r\n")))
(def println
(fn* println [& args] (do (display args) (print " ") (newline))))
;during bootstrap we don't have destructuring let, loop or fn
(def
^{:macro true
:added "1.0"}
loop1 (fn* loop1 [& decl] (cons 'loop* decl)))
(def
^{:macro true
:added "1.0"}
fn0 (fn* fn0 [& decl]
(with-meta (cons 'fn* decl)
(meta &form))))
(def
^{:doc "Same as (first (first x))"
:arglists '([x]) }
ffirst (fn0 ffirst [x] (first (first x))))
(def
^{:doc "Same as (next(first x))"
:arglists '([x]) }
nfirst (fn0 nfirst [x] (next (first x))))
(def
^{:doc "Same as (first (next x))"
:arglists '([x]) }
fnext (fn0 fnext [x] (first (next x))))
(def
^{:doc "Same as (next (next x))"
:arglists '([x]) }
nnext (fn0 nnext [x] (next (next x))))
(def
^{:arglists '([coll])
:doc "Return the last item in coll"
}
last (fn0 last [s]
(if (next s)
(recur (next s))
(first s))))
;(def second (fn0 second [xs] (nth xs 1)))
(def second (fn0 second [xs] (first (next xs))))
(def
^{:arglists '([coll])
:doc "Return a seq of all but the last item in coll, in linear time"
}
butlast (fn0 butlast [s]
(loop1 [ret [] s s]
(if (next s)
(recur (conj ret (first s)) (next s))
(seq ret)))))
;will redefine later
(def
^{:macro true
:added "1.0"}
fn (fn* fn [& decl]
(with-meta (cons 'fn* decl)
(meta &form))))
(def
^{:doc "Same as (def name (fn0 [params* ] exprs*)) or (def
name (fn0 ([params* ] exprs*)+)) with any doc-string or attrs added
to the var metadata. prepost-map defines a map with optional keys
:pre and :post that contain collections of pre or post conditions."
:arglists '([name doc-string? attr-map? [params*] prepost-map? body]
[name doc-string? attr-map? ([params*] prepost-map? body)+ attr-map?])
:macro true
:added "1.0"}
defn0 (fn0 defn0 [name & fdecl]
(if (symbol? name)
nil
(throw (System.ArgumentException. "First argument to defn0 must be a symbol"))) ;;; IllegalArgumentException
(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 (if (meta name) (meta name) {}) m)]
(list 'def (with-meta name m)
(cons `fn0 fdecl) ))))
(defn0 inc [n] (+ n 1))
(defn0 dec [n] (- n 1))
(defn0 pos? [n] (< 0 n))
(defn0 neg? [n] (> 0 n))
(defn0 false?
"Returns true if x is the value false, false otherwise."
{:tag Boolean
:added "1.0"}
[x] (Inc.Util/identical x false))
(defn0 true?
"Returns true if x is the value true, false otherwise."
{:tag Boolean
:added "1.0"}
[x] (Inc.Util/identical x true))
(defn0 not
"Returns true if x is logical false, false otherwise."
{:tag Boolean
:added "1.0"}
[x] (if x false true))
(defn0 <= [a b] (not (> a b)))
(defn0 >= [a b] (not (< a b)))
(defn0 ref-set [r x] (alter r (fn0 [_] x)))
(defn0
^{: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
([map key val] (assoc* map key val))
([map key val & kvs]
(let* [ret (assoc map key val)]
(if kvs
(if (next kvs)
(recur ret (first kvs) (second kvs) (nnext kvs))
(throw (System.ArgumentException. ;;; IllegalArgumentException
"assoc expects even number of arguments after map/vector, found odd number")))
ret)))
)
(def
^{:doc "Like defn0, 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?])
:added "1.0"}
defmacro (fn0 [name & args]
(let* [prefix (loop1 [p (list name) args args]
(let* [f (first args)]
(if (string? f)
(recur (cons f p) (next args))
(if (map? f)
(recur (cons f p) (next args))
p))))
fdecl (loop1 [fd args]
(if (string? (first fd))
(recur (next fd))
(if (map? (first fd))
(recur (next fd))
fd)))
fdecl (if (vector? (first fdecl))
(list fdecl)
fdecl)
add-args (fn0 [acc ds]
(if (nil? ds)
acc
(let* [d (first ds)]
(if (map? d)
(conj acc d)
(recur (conj acc d) (next ds))))))
fdecl (seq (add-args [] fdecl))
decl (loop1 [p prefix d fdecl]
(if p
(recur (next p) (cons (first p) d))
d))
]
(list 'do
(cons `defn0 decl)
(list 'set-macro name)
(list 'var name)))))
(set-macro defmacro)
(defmacro comment [& body] )
(defmacro locking
"Executes exprs in an implicit do, while holding the monitor of x.
Will release the monitor of x in all circumstances."
{:added "1.0"}
[x & body]
`(let* [lockee# ~x]
(try
(monitor-enter lockee#)
~@body
(finally
(monitor-exit lockee#)))))
(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."
{:added "1.0"}
([] 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."
{:added "1.0"}
([] nil)
([x] x)
([x & next]
`(let* [or# ~x]
(if or# or# (or ~@next)))))
;(defn0 >= [a b] (or (> a b) (= a b)))
;(defn0 <= [a b] (or (< a b) (= a b)))
(defn0 xor [a b] (and (or a b) (not (and a b))))
(defmacro commute [r f & xs] `(alter ~r ~f ~@xs))
(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."
{:added "1.0"}
([x form] `(. ~x ~form))
([x form & more] `(.. (. ~x ~form) ~@more)))
;(defmacro list [& xs] `(map eval (quote ~xs)))
(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
second item in second form, etc."
{:added "1.0"}
([x] x)
([x form] (if (seq? form)
;(with-meta `(~(first form) ~x ~@(next form)) (meta form))
`(~(first form) ~x ~@(next form))
(list form x)))
([x form & more] `(-> (-> ~x ~form) ~@more)))
(defmacro ->>
"Threads the expr through the forms. Inserts x as the
last 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
last item in second form, etc."
{:added "1.1"}
([x form] (if (seq? form)
(with-meta `(~(first form) ~@(next form) ~x) (meta form))
(list form x)))
([x form & more] `(->> (->> ~x ~form) ~@more)))
(defmacro when
"Evaluates test. If logical true, evaluates body in an implicit do."
{:added "1.0"}
[test & body]
(list 'if test (cons 'do body) nil)
)
(defmacro when-not
"Evaluates test. If logical false, evaluates body in an implicit do."
{:added "1.0"}
[test & body]
(list 'if test nil (cons 'do body))
)
(defn0 spread
{:private true}
[arglist]
(cond
(nil? arglist) nil
(nil? (next arglist)) (seq (first arglist))
:else (cons (first arglist) (spread (next arglist)))))
(defn0 list*
"Creates a new list containing the items prepended to the rest, the
last of which will be treated as a sequence."
([args] (seq args))
([a args] (cons a args))
([a b args] (cons a (cons b args)))
([a b c args] (cons a (cons b (cons c args))))
([a b c d & more]
(cons a (cons b (cons c (cons d (spread more)))))))
(defn0 map
"Returns a lazy sequence 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."
{:added "1.0"
:static true}
([f coll]
(lazy-seq
(when-let [s (seq coll)]
(cons (f (first s)) (map f (rest s))))))
([f c1 c2]
(lazy-seq
(let* [s1 (seq c1) s2 (seq c2)]
(when (and s1 s2)
(cons (f (first s1) (first s2))
(map f (rest s1) (rest s2)))))))
([f c1 c2 c3]
(lazy-seq
(let* [s1 (seq c1) s2 (seq c2) s3 (seq c3)]
(when (and s1 s2 s3)
(cons (f (first s1) (first s2) (first s3))
(map f (rest s1) (rest s2) (rest s3)))))))
([f c1 c2 c3 & colls]
(let* [step (fn0 step [cs]
(lazy-seq
(let* [ss (map seq cs)]
(when (every? identity ss)
(cons (map first ss) (step (map rest ss)))))))]
(map #(apply f %) (step (conj colls c3 c2 c1))))))
(defmacro ^{:private true} assert-args
[& pairs]
`(do (when-not ~(first pairs)
(throw (System.ArgumentException. ;;;IllegalArgumentException.
(str (first ~'&form) " requires " ~(second pairs) " in " ~'*ns* ))))
~(let* [more (nnext pairs)]
(when more
(list* `assert-args more))))
)
(defn0 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)))
(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)))
(defn0 not=
"Same as (not (= obj1 obj2))"
{:tag Boolean
:added "1.0"
:static true}
([x] false)
([x y] (not (= x y)))
([x y & more]
(not (apply = x y more))))
(defmacro if-let
"bindings => binding-form test
If test is true, evaluates then with binding-form bound to the value of
test, if not, yields else"
{:added "1.0"}
([bindings then]
`(if-let ~bindings ~then nil))
([bindings then else & oldform]
(assert-args
(vector? bindings) "a vector for its binding"
(nil? oldform) "1 or 2 forms after binding vector"
(== 2 (count bindings)) "exactly 2 forms in binding vector"
)
(let* [form (bindings 0) tst (bindings 1)]
`(let* [temp# ~tst]
(if temp#
(let* [~form temp#]
~then)
~else)))
)
)
(defmacro when-let
"bindings => binding-form test
When test is true, evaluates body with binding-form bound to the value of test"
{:added "1.0"}
[bindings & body]
(assert-args
(vector? bindings) "a vector for its binding"
(== 2 (count bindings)) "exactly 2 forms in binding vector")
(let* [form (bindings 0) tst (bindings 1)]
`(let* [temp# ~tst]
(when temp#
(let* [~form temp#]
~@body)))))
(defn0 range
"Returns a lazy seq of nums from start (inclusive) to end
(exclusive), by step, where start defaults to 0 and step to 1, and end
to infinity."
([] (range 0 System.Int64/MaxValue 1))
([end] (range 0 end 1))
([start end] (range start end 1))
([start end step]
(lazy-seq
(let* [comp (if (pos? step) < >)]
(if (comp start end)
(cons start (range (+ start step) end step)))))))
(defn0 take
"Returns a lazy sequence of the first n items in coll, or all items if
there are fewer than n."
[n coll]
(lazy-seq
(when (pos? n)
(when-let [s (seq coll)]
(cons (first s) (take (dec n) (rest s))))))
)
(defn0 filter
"Returns a lazy sequence of the items in coll for which
(pairs item) returns true. pred must be free of side-effects."
[pred coll]
(let* [step (fn0 [p c]
(when-let [s (seq c)]
(if (p (first s))
(cons (first s) (filter p (rest s)))
(recur p (rest s)))))]
(lazy-seq (step pred coll))))
(defn0 dorun
"When lazy sequences are produced via functions that have side
effects, any effects other than those needed to produce the first
element in the seq do not occur until the seq is consumed. dorun can
be used to force any effects. Walks through the successive nexts of
the seq, does not retain the head and returns nil."
{:added "1.0"
:static true}
([coll]
(when (seq coll)
; (println "dorun : " (first coll))
(recur (next coll))))
([n coll]
(when (and (seq coll) (pos? n))
(recur (dec n) (next coll)))))
(defn0 doall
"When lazy sequences are produced via functions that have side
effects, any effects other than those needed to produce the first
element in the seq do not occur until the seq is consumed. doall can
be used to force any effects. Walks through the successive nexts of
the seq, retains the head and returns it, thus causing the entire
seq to reside in memory at one time."
{:added "1.0"
:static true}
([coll]
(dorun coll)
coll)
([n coll]
(dorun n coll)
coll))
(defn0 nthnext
"Returns the nth next of coll, (seq coll) when n is 0."
{:added "1.0"
:static true}
[coll n]
(loop1 [n n xs (seq coll)]
(if (and xs (pos? n))
(recur (dec n) (next xs))
xs)))
(defn0 nthrest
"Returns the nth rest of coll, coll when n is 0."
{:added "1.3"
:static true}
[coll n]
(loop1 [n n xs coll]
(if (and (pos? n) (seq xs))
(recur (dec n) (rest xs))
xs)))
(defn0 partition
"Returns a lazy sequence of lists of n items each, at offsets step
apart. If step is not supplied, defaults to n, i.e. the partitions
do not overlap. If a pad collection is supplied, use its elements as
necessary to complete last partition upto n items. In case there are
not enough padding elements, return a partition with less than n items."
{:added "1.0"
:static true}
([n coll]
(partition n n coll))
([n step coll]
(lazy-seq
(when-let [s (seq coll)]
(let* [p (doall (take n s))]
(when (= n (count p))
(cons p (partition n step (nthrest s step))))))))
([n step pad coll]
(lazy-seq
(when-let [s (seq coll)]
(let* [p (doall (take n s))]
(if (= n (count p))
(cons p (partition n step pad (nthrest s step)))
(list (take n (concat p pad)))))))))
(defn0 reduce1
([f coll]
(let* [s (seq coll)]
(if s
(reduce1 f (first s) (next s))
(f))))
([f val coll]
(let* [s (seq coll)]
(if s
(recur f (f val (first s)) (next s))
val))))
; fold function (f) over seq (xs) while accumulating (a)
(defn0 fold [f a xs]
(let* [x (first xs)]
(if (not x)
a
(fold f (f x a) (rest xs)))
))
; (define reverse (fn0 [xs] (fold cons '() xs)))
(defn0 reverse [xs] (fold cons [] xs))
(defmacro while
"Repeatedly executes body while test expression is true. Presumes
some side-effect will cause test to become false/nil. Returns nil"
{:added "1.0"}
[test & body]
`(loop1 []
(when ~test
~@body
(recur))))
(defn0 push-thread-bindings
"WARNING: This is a low-level function. Prefer high-level macros like
binding where ever possible.
Takes a map of Var/value pairs. Binds each Var to the associated value for
the current thread. Each call *MUST* be accompanied by a matching call to
pop-thread-bindings wrapped in a try-finally!
(push-thread-bindings bindings)
(try
...
(finally
(pop-thread-bindings)))"
{:added "1.1"
:static true}
[bindings]
(inc.Util/pushThreadBindings bindings))
(defn0 pop-thread-bindings
"Pop one set of bindings pushed with push-binding before. It is an error to
pop bindings without pushing before."
{:added "1.1"
:static true}
[]
(inc.Util/popThreadBindings))
(defn0 get-thread-bindings
"Get a map with the Var/value pairs which is currently in effect for the
current thread."
{:added "1.1"
:static true}
[]
(inc.Util/getThreadBindings))
(defn0 with-bindings*
"Takes a map of Var/value pairs. Installs for the given Vars the associated
values as thread-local bindings. Then calls f with the supplied arguments.
Pops the installed bindings after f returned. Returns whatever f returns."
{:added "1.1"
:static true}
[binding-map f & args]
(push-thread-bindings binding-map)
(try
(apply f args)
(finally
(pop-thread-bindings))))
(defmacro with-bindings
"Takes a map of Var/value pairs. Installs for the given Vars the associated
values as thread-local bindings. The executes body. Pops the installed
bindings after body was evaluated. Returns the value of body."
{:added "1.1"}
[binding-map & body]
`(with-bindings* ~binding-map (fn0 [] ~@body)))
(defn0 bound-fn*
"Returns a function, which will install the same bindings in effect as in
the thread at the time bound-fn* was called and then call f with any given
arguments. This may be used to define a helper function which runs on a
different thread, but needs the same bindings in place."
{:added "1.1"
:static true}
[f]
(let* [bindings (get-thread-bindings)]
(fn0 [& args]
(apply with-bindings* bindings f args))))
(defmacro bound-fn
"Returns a function defined by the given fntail, which will install the
same bindings in effect as in the thread at the time bound-fn was called.
This may be used to define a helper function which runs on a different
thread, but needs the same bindings in place."
{:added "1.1"}
[& fntail]
`(bound-fn* (fn0 ~@fntail)))
(def sum (fn0 [xs] (fold + 0 xs)))
(def odd? (fn0 [x] (= 1 (% x 2))))
(def even? (fn0 [x] (= 0 (% x 2))))
;(defn0 require [e] (if e e (amb)))
(defn0 cdr [x] (rest x))
(defn0 car [x] (first x))
(defn0 member? [item lst]
(if lst
(if (= item (car lst))
true
(member? item (cdr lst)))
false))
(defn0 distinct? [lst]
(if lst
(if (member? (car lst) (cdr lst))
false
(distinct? (cdr lst)))
true))
(defn0 exclude [items lst]
(if lst
(if (member? (car lst) items)
(exclude items (cdr lst))
(cons (car lst) (exclude items (cdr lst))))
()))
(defn0 not=
"Same as (not (= obj1 obj2))"
{:tag Boolean
:added "1.0"
:static true}
([x] false)
([x y] (not (= x y)))
([x y & more]
(not (apply = x y more))))
(defn0 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."
{:added "1.0"
:static true}
([] identity)
([f] f)
([f g]
(fn0
([] (f (g)))
([x] (f (g x)))
([x y] (f (g x y)))
([x y z] (f (g x y z)))
([x y z & args] (f (apply g x y z args)))))
([f g h]
(fn0
([] (f (g (h))))
([x] (f (g (h x))))
([x y] (f (g (h x y))))
([x y z] (f (g (h x y z))))
([x y z & args] (f (g (apply h x y z args))))))
([f1 f2 f3 & fs]
(let* [fs (reverse (list* f1 f2 f3 fs))]
(fn0 [& args]
(loop1 [ret (apply (first fs) args) fs (next fs)]
(if fs
(recur ((first fs) ret) (next fs))
ret))))))
(defn0 concat
"Returns a lazy seq representing the concatenation of the elements in the supplied colls."
{:added "1.0"
:static true}
([] (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 (fn0 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))))
;;;;;;;;;;;;;;;;;;; sequence fns ;;;;;;;;;;;;;;;;;;;;;;;
(defn0 sequence
"Coerces coll to a (possibly empty) sequence, if it is not already
one. Will not force a lazy seq. (sequence nil) yields ()"
{:added "1.0"
:static true}
[coll]
(if (seq? coll) coll
(or (seq coll) ())))
(defn0 every?
"Returns true if (pred x) is logical true for every x in coll, else
false."
{:tag Boolean
:added "1.0"
:static true}
[pred coll]
(cond
(nil? (seq coll)) true
(pred (first coll)) (recur pred (next coll))
:else false))
(def
^{:tag Boolean
:doc "Returns false if (pred x) is logical true for every x in
coll, else true."
:arglists '([pred coll])
:added "1.0"}
not-every? (comp not every?))
(defn0 some
"Returns the first logical true value of (pred x) for any x in coll,
else nil. One common idiom is to use a set as pred, for example
this will return :fred if :fred is in the sequence, otherwise nil:
(some #{:fred} coll)"
{:added "1.0"
:static true}
[pred coll]
(when (seq coll)
(or (pred (first coll)) (recur pred (next coll)))))
(def
^{:tag Boolean
:doc "Returns false if (pred x) is logical true for any x in coll,
else true."
:arglists '([pred coll])
:added "1.0"}
not-any? (comp not some))
(defn0 max
"Returns the greatest of the nums."
([x] x)
([x y] (if (< x y) y x))
([x y & more]
(reduce1 max (max x y) more)))
(defn0 min
"Returns the least of the nums."
([x] x)
([x y] (if (> x y) y x))
([x y & more]
(reduce1 min (min x y) more)))
(defmacro dotimes
"bindings => name n
Repeatedly executes body (presumably for side-effects) with name
bound to integers from 0 through n-1."
{:added "1.0"}
[bindings & body]
(let* [i (first bindings)
n (second bindings)]
`(let* [n# ~n]
(loop1 [~i 0]
(when (< ~i n#)
~@body
(recur (inc ~i)))))))
(defn0 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)))
(defn0 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]
(fn0
([] (not (f)))
([x] (not (f x)))
([x y] (not (f x y)))
([x y & zs] (not (apply f x y zs)))))
(defn0 constantly
"Returns a function that takes any number of arguments and returns x."
[x] (fn0 [& args] x))
(defn0 remove
"Returns a lazy sequence of the items in coll for which
(pred item) returns false. pred must be free of side-effects."
[pred coll]
(filter (complement pred) coll))
(defn0 take-while
"Returns a lazy sequence of successive items from coll while
(pred item) returns true. pred must be free of side-effects."
[pred coll]
(lazy-seq
(when-let [s (seq coll)]
(when (pred (first s))
(cons (first s) (take-while pred (rest s)))))))
(defn0 drop
"Returns a lazy sequence of all but the first n items in coll."
[n coll]
(let* [step (fn0 [n coll]
(let* [s (seq coll)]
(if (and (pos? n) s)
(recur (dec n) (rest s))
s)))]
(lazy-seq (step n coll))))
(defn0 drop-last
"Return a lazy sequence of all but the last n (default 1) items in coll"
([s] (drop-last 1 s))
([n s] (map (fn0 [x _] x) s (drop n s))))
(defn0 take-last
"Returns a seq of the last n items in coll. Depending on the type
of coll may be no better than linear time. For vectors, see also subvec."
{:added "1.1"
:static true}
[n coll]
(loop1 [s (seq coll), lead (seq (drop n coll))]
(if lead
(recur (next s) (next lead))
s)))
(defn0 drop-while
"Returns a lazy sequence of the items in coll starting from the first
item for which (pred item) returns logical false."
{:added "1.0"
:static true}
[pred coll]
(let* [step (fn0 [pred coll]
(let* [s (seq coll)]
(if (and s (pred (first s)))
(recur pred (rest s))
s)))]
(lazy-seq (step pred coll))))
(defn0 cycle
"Returns a lazy (infinite!) sequence of repetitions of the items in coll."
{:added "1.0"
:static true}
[coll] (lazy-seq
(when-let [s (seq coll)]
(concat s (cycle s)))))
(defn0 split-at
"Returns a vector of [(take n coll) (drop n coll)]"
[n coll]
[(take n coll) (drop n coll)])
(defn0 split-with
"Returns a vector of [(take-while pred coll) (drop-while pred coll)]"
[pred coll]
[(take-while pred coll) (drop-while pred coll)])
(defn0 repeat
"Returns a lazy (infinite! or length n if supplied) sequence of xs."
([x] (lazy-seq (cons x (repeat x))))
([n x] (take n (repeat x))))
(defn0 iterate
"Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of side-effects"
[f x] (lazy-seq (cons x (iterate f (f x)))))
(defmacro ns
"Sets *ns* to the namespace named by name (unevaluated), creating it
if needed. references can be zero or more of: (:refer-inc ...)
(:require ...) (:use ...) (:import ...) (:load ...)
with the syntax of refer-inc/require/use/import/load
respectively, except the arguments are unevaluated and need not be
quoted. If :refer-inc is not used, a
default (refer 'inc) is used. Use of ns is preferred to
individual calls to in-ns/require/use/import:
(ns foo.bar
(:refer-inc :exclude [ancestors printf])
(:require (inc.contrib sql combinatorics))
(:use (my.lib this that))
(:import (Inc.Util Date Timer Random)
(java.sql Connection Statement)))"
{:arglists '([name docstring? attr-map? references*])
:added "1.0"}
[name & references]
(let* [process-reference (fn0 [[kname & args]]
`(~(symbol "inc.core" (inc.core/name kname))
~@(map #(list 'quote %) args)))
docstring (when (string? (first references)) (first references))
references (if docstring (next references) references)
name (if docstring
(vary-meta name assoc :doc docstring)
name)
metadata (when (map? (first references)) (first references))
references (if metadata (next references) references)
name (if metadata
(vary-meta name merge metadata)
name)
]
`(do
(in-ns '~name)
~@(when (and (not= name 'inc.core) (not-any? #(= :refer-inc (first %)) references))
`((inc.core/refer '~'inc.core)))
~@(map process-reference references))
(if (.Equals '~name 'inc.core)
nil
(do (dosync (commute @#'*loaded-libs* conj '~name)) nil))))
(defn0 dissoc