forked from pixie-lang/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib.pxi
3098 lines (2732 loc) · 96.9 KB
/
stdlib.pxi
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
(in-ns :pixie.stdlib)
(def libc (ffi-library pixie.platform/lib-c-name))
(def exit (ffi-fn libc "exit" [CInt] CInt))
(def puts (ffi-fn libc "puts" [CCharP] CInt))
(def sh (ffi-fn libc "system" [CCharP] CInt))
(def printf (ffi-fn libc "printf" [CCharP] CInt :variadic? true))
(def getenv (ffi-fn libc "getenv" [CCharP] CCharP))
(def libedit (ffi-library (str "libedit." pixie.platform/so-ext)))
(def readline (ffi-fn libedit "readline" [CCharP] CCharP))
(def rand (ffi-fn libc "rand" [] CInt))
(def srand (ffi-fn libc "srand" [CInt] CInt))
(def fopen (ffi-fn libc "fopen" [CCharP CCharP] CVoidP))
(def fread (ffi-fn libc "fread" [CVoidP CInt CInt CVoidP] CInt))
(def mkdtemp (ffi-fn libc "mkdtemp" [CCharP] CCharP))
(def mkdir (ffi-fn libc "mkdir" [CCharP] CCharP))
(def rmdir (ffi-fn libc "rmdir" [CCharP] CCharP))
(def rm (ffi-fn libc "remove" [CCharP] CCharP))
(def libm (ffi-library (str "libm." pixie.platform/so-ext)))
(def atan2 (ffi-fn libm "atan2" [CDouble CDouble] CDouble))
(def lround (ffi-fn libm "lround" [CDouble] CInt))
(def reset! -reset!)
(def program-arguments [])
(def fn (fn* [& args]
(cons 'fn* args)))
(set-macro! fn)
(def let (fn* [& args]
(cons 'let* args)))
(set-macro! let)
(def loop (fn* [& args]
(cons 'loop* args)))
(set-macro! loop)
(def identity
(fn ^{:doc "The identity function. Returns its argument."
:added "0.1"}
identity
[x]
x))
(def conj
(fn ^{:doc "Adds elements to the collection. Elements are added to the end except in the case of Cons lists."
:signatures [[] [coll] [coll item] [coll item & args]]
:added "0.1"}
conj
([] [])
([coll] coll)
([coll item] (-conj coll item))
([coll item & args]
(reduce -conj (-conj coll item) args))))
(def conj!
(fn ^{:doc "Adds elements to the transient collection. Elements are added to the end except in the case of Cons lists."
:signatures [[] [coll] [coll item] [coll item & args]]
:added "0.1"}
conj!
([] (-transient []))
([coll] (-persistent! coll))
([coll item] (-conj! coll item))
([coll item & args]
(reduce -conj! (-conj! coll item) args))))
(def disj (fn ^{:doc "Removes elements from the collection."
:signatures [[] [coll] [coll item]]
:added "0.1"}
disj
([] [])
([coll] coll)
([coll item] (-disj coll item))
([coll item & items]
(reduce -disj (-disj coll item) items))))
(comment (def disj! (fn ^{:doc "Removes elements from the transient collection."
:signatures [[] [coll] [coll item] [coll item & items]]
:added "0.1"}
disj!
([] (-transient []))
([result] (-persistent! result))
([result item] (-disj! result item))
([coll item & items]
(reduce -disj! (-disj! coll item) items)))))
(def pop
(fn ^{:doc "Pops elements off a stack."
:signatures [[] [coll] [coll item] [coll item & args]]
:added "0.1"}
pop
([coll] (-pop coll))))
(def push
(fn ^{:doc "Pushes an element on to a stack."
:signatures [[] [coll] [coll item] [coll item & args]]
:added "0.1"}
push
([coll x] (-push coll x))))
(def pop!
(fn ^{:doc "Pops elements off a transient stack."
:signatures [[] [coll] [coll item] [coll item & args]]
:added "0.1"}
pop!
([coll] (-pop! coll))))
(def push!
(fn ^{:doc "Pushes an element on to a transient stack."
:signatures [[] [coll] [coll item] [coll item & args]]
:added "0.1"}
push!
([coll x] (-push! coll x))))
(def transient (fn [coll] (-transient coll)))
(def persistent! (fn [coll] (-persistent! coll)))
(def transduce (fn transduce
([f coll]
(let [result (-reduce coll f (f))]
(f result)))
([xform rf coll]
(let [f (xform rf)
result (-reduce coll f (f))]
(f result)))
([xform rf init coll]
(let [f (xform rf)
result (-reduce coll f init)]
(f result)))))
(def map (fn ^{:doc "Creates a transducer that applies f to every input element."
:signatures [[f] [f coll]]
:added "0.1"}
map
([f]
(fn [xf]
(fn
([] (xf))
([result] (xf result))
([result item] (xf result (f item))))))
([f coll]
(lazy-seq*
(fn []
(let [s (seq coll)]
(if s
(cons (f (first s))
(map f (rest s)))
nil)))))
([f & colls]
(let [step (fn step [cs]
(lazy-seq*
(fn []
(let [ss (map seq cs)]
(if (every? identity ss)
(cons (map first ss) (step (map rest ss)))
nil)))))]
(map (fn [args] (apply f args)) (step colls))))))
(def reduce (fn
([rf col]
(reduce rf (rf) col))
([rf init col]
(-reduce col rf init))))
(def instance? (fn ^{:doc "Checks if x is an instance of t.
When t is seqable, checks if x is an instance of
any of the types contained therein."
:signatures [[t x]]}
instance? [t x]
(if (-satisfies? ISeqable t)
(let [ts (seq t)]
(if (not ts) false
(if (-instance? (first ts) x)
true
(instance? (rest ts) x))))
(-instance? t x))))
(def satisfies? (fn ^{:doc "Checks if x satisfies the protocol p.
When p is seqable, checks if x satisfies all of
the protocols contained therein."
:signatures [[t x]]}
satisfies? [p x]
(if (-satisfies? ISeqable p)
(let [ps (seq p)]
(if (not ps) true
(if (not (-satisfies? (first ps) x))
false
(satisfies? (rest ps) x))))
(-satisfies? p x))))
(def into (fn ^{:doc "Add the elements of `from` to the collection `to`."
:signatures [[to from] [to xform from]]
:added "0.1"}
([to from]
(if (satisfies? IToTransient to)
(persistent! (reduce conj! (transient to) from))
(reduce conj to from)))
([to xform from]
(if (satisfies? IToTransient to)
(transduce xform conj! (transient to) from)
(transduce xform conj to from)))))
(def interpose
(fn ^{:doc "Returns a transducer that inserts `val` in between elements of a collection."
:signatures [[val] [val coll]]
:added "0.1"}
interpose
([val] (fn [xf]
(let [first? (atom true)]
(fn
([] (xf))
([result] (xf result))
([result item] (if @first?
(do (reset! first? false)
(xf result item))
(xf (xf result val) item)))))))
([val coll]
(transduce (interpose val) conj coll))))
(def preserving-reduced
(fn preserving-reduced [rf]
(fn pr-inner [a b]
(let [ret (rf a b)]
(if (reduced? ret)
(reduced ret)
ret)))))
(def cat
(fn ^{:doc "A transducer that concatenates elements of a collection."
:added "0.1"}
cat
[rf]
(let [rrf (preserving-reduced rf)]
(fn cat-inner
([] (rf))
([result] (rf result))
([result input]
(reduce rrf result input))))))
(def mapcat
(fn ^{:doc "Maps f over the elements of coll and concatenates the result."
:added "0.1"}
mapcat
([f]
(comp (map f) cat))
([f coll]
(transduce (mapcat f) conj coll))))
(def seq-reduce (fn seq-reduce
[coll f init]
(loop [init init
coll (seq coll)]
(if (reduced? init)
@init
(if (seq coll)
(recur (f init (first coll))
(seq (next coll)))
init)))))
(def indexed-reduce
(fn indexed-reduce
[coll f init]
(let [max (count coll)]
(loop [init init
i 0]
(if (reduced? init)
@init
(if (-eq i max)
init
(recur (f init (nth coll i nil)) (+ i 1))))))))
(def rest (fn ^{:doc "Returns the elements after the first element, or () if there are no more elements."
:signatures [[coll]]
:added "0.1"}
[coll]
(let [next (next coll)]
(if next next '()))))
;; Make all Function types extend IFn
(extend -invoke Code -invoke)
(extend -invoke NativeFn -invoke)
(extend -invoke VariadicCode -invoke)
(extend -invoke MultiArityFn -invoke)
(extend -invoke Closure -invoke)
(extend -invoke Var -invoke)
(extend -invoke PolymorphicFn -invoke)
(extend -invoke DoublePolymorphicFn -invoke)
(extend -reduce Cons seq-reduce)
(extend -reduce PersistentList seq-reduce)
(extend -reduce LazySeq seq-reduce)
(comment (extend -reduce Array indexed-reduce))
(extend -reduce Buffer indexed-reduce)
(extend -reduce String indexed-reduce)
(extend -str Bool
(fn [x]
(if (identical? x true)
"true"
"false")))
(extend -repr Bool -str)
(extend -str Nil (fn [x] "nil"))
(extend -repr Nil -str)
(extend -reduce Nil (fn [self f init] init))
(extend -hash Nil (fn [self] 100000))
(extend -with-meta Nil (fn [self _] nil))
(extend -deref Nil (fn [_] nil))
(extend -contains-key Nil (fn [_ _] false))
(extend -hash Integer hash-int)
(extend -eq Integer -num-eq)
(extend -eq Float -num-eq)
(extend -eq Ratio -num-eq)
(def ordered-hash-reducing-fn
(fn ordered-hash-reducing-fn
([] (new-hash-state))
([state] (finish-hash-state state))
([state itm] (update-hash-ordered! state itm))))
(def unordered-hash-reducing-fn
(fn unordered-hash-reducing-fn
([] (new-hash-state))
([state] (finish-hash-state state))
([state itm] (update-hash-unordered! state itm))))
(def string-builder
(fn ^{:doc "Creates a reducing function that builds a string based on calling str on the transduced collection."}
([] (-string-builder))
([sb] (str sb))
([sb item] (conj! sb item))))
(extend -str PersistentVector
(fn [v]
(str "[" (transduce (interpose " ") string-builder v) "]")))
(extend -repr PersistentVector
(fn [v]
(str "[" (transduce (comp (map -repr) (interpose " ")) string-builder v) "]")))
(extend -str Cons
(fn [v]
(str "(" (transduce (interpose " ") string-builder v) ")")))
(extend -repr Cons
(fn [v]
(str "(" (transduce (comp (map -repr) (interpose " ")) string-builder v) ")")))
(extend -hash Cons
(fn [v]
(transduce ordered-hash-reducing-fn v)))
(extend -str PersistentList
(fn [v]
(str "(" (transduce (interpose " ") string-builder v) ")")))
(extend -repr PersistentList
(fn [v]
(str "(" (transduce (comp (map -repr) (interpose " ")) string-builder v) ")")))
(extend -hash PersistentList
(fn [v]
(transduce ordered-hash-reducing-fn v)))
(extend -str LazySeq
(fn [v]
(str "(" (transduce (interpose " ") string-builder v) ")")))
(extend -repr LazySeq
(fn [v]
(str "(" (transduce (comp (map -repr) (interpose " ")) string-builder v) ")")))
(extend -hash PersistentVector
(fn [v]
(transduce ordered-hash-reducing-fn v)))
(extend -hash PersistentHashSet
(fn [v]
(transduce ordered-hash-reducing-fn v)))
(add-marshall-handlers PersistentHashSet
(fn [obj] (vec obj))
(fn [obj] (apply hash-set obj)))
(extend -hash PersistentHashMap
(fn [v]
(transduce ordered-hash-reducing-fn v)))
(extend -hash EmptyList (fn [v] 5555555))
(extend -hash Bool
(fn [v]
(if v
1111111
3333333)))
(def = -eq)
(extend -seq PersistentVector
(fn vector-seq
([self]
(vector-seq self 0))
([self x]
(if (= x (count self))
nil
(cons (nth self x) (lazy-seq* (fn [] (vector-seq self (+ x 1)))))))))
(extend -seq String
(fn string-seq
([self]
(string-seq self 0))
([self x]
(if (= x (count self))
nil
(cons (nth self x) (lazy-seq* (fn [] (string-seq self (+ x 1)))))))))
(def concat
(fn ^{:doc "Concatenates its arguments."
:signatures [[& args]]
:added "0.1"}
concat
[& args] (transduce cat conj args)))
(def key (fn [x] (-key x)))
(def val (fn [x] (-val x)))
(def defn (fn ^{:doc "Defines a new function."
:signatures [[nm doc? meta? & body]]}
defn
[nm & rest]
(let [meta (if (meta nm) (meta nm) {})
meta (if (instance? String (first rest))
(assoc meta :doc (first rest))
meta)
rest (if (instance? String (first rest)) (next rest) rest)
meta (if (satisfies? IMap (first rest))
(merge meta (first rest))
meta)
rest (if (satisfies? IMap (first rest)) (next rest) rest)
meta (if (-contains-key meta :signatures)
meta
(merge meta {:signatures
(if (satisfies? IVector (first rest))
[(first rest)]
(transduce (map first) conj rest))}))
nm (with-meta nm meta)]
`(def ~nm (fn ~nm ~@rest)))))
(set-macro! defn)
(defn defmacro
{:doc "Defines a new macro."
:added "0.1"}
[nm & rest]
`(do (defn ~nm ~@rest)
(set-macro! ~nm)
~nm))
(set-macro! defmacro)
(defmacro defn-
{:doc "Define a new non-public function. Otherwise the same as defn."
:signatures [[nm doc? meta? & body]]
:added "0.1"}
[nm & rest]
(let [nm (with-meta nm (assoc (meta nm) :private true))]
(cons `defn (cons nm rest))))
(defn not
{:doc "Inverts the input, if a truthy value is supplied, returns false, otherwise
returns true."
:signatures [[x]]
:added "0.1"}
[x]
(if x false true))
(defn +
{:doc "Adds the arguments, returning 0 if no arguments."
:signatures [[& args]]
:added "0.1"}
([] 0)
([x] x)
([x y] (-add x y))
([x y & args]
(reduce -add (-add x y) args)))
(defn -
([] 0)
([x] (-sub 0 x))
([x y] (-sub x y))
([x y & args]
(reduce -sub (-sub x y) args)))
(defn *
([] 1)
([x] x)
([x y] (-mul x y))
([x y & args]
(reduce -mul (-mul x y) args)))
(defn unchecked-add
{:doc "Adds the arguments, returning 0 if no arguments."
:signatures [[& args]]
:added "0.1"}
([] 0)
([x] x)
([x y] (-unchecked-add x y))
([x y & args]
(reduce -unchecked-add (-unchecked-add x y) args)))
(defn unchecked-subtract
([] 0)
([x] (-unchecked-subtract 0 x))
([x y] (-unchecked-subtract x y))
([x y & args]
(reduce -unchecked-subtract (-unchecked-subtract x y) args)))
(defn unchecked-multiply
([] 1)
([x] x)
([x y] (-unchecked-multiply x y))
([x y & args]
(reduce -unchecked-multiply (-unchecked-multiply x y) args)))
(defn /
([x] (-div 1 x))
([x y] (-div x y))
([x y & args]
(reduce -div (-div x y) args)))
(defn quot [num div]
(-quot num div))
(defn rem [num div]
(-rem num div))
(defn rand-int
{:doc "Returns a random integer between 0 (inclusive) and n (exclusive)."}
[n]
(if (zero? n)
0
(rem (rand) n)))
(defn =
{:doc "Returns true if all the arguments are equivalent. Otherwise, returns false. Uses
-eq to perform equality checks."
:signatures [[& args]]
:added "0.1"}
([x] true)
([x y] (eq x y))
([x y & rest] (if (eq x y)
(apply = y rest)
false)))
(defn not=
{:doc "Returns true if one (or more) of the arguments are not equivalent to the others. Uses
-eq to perform equality checks."
:signatures [[& args]]
:added "0.1"}
([x] false)
([x y] (not (eq x y)))
([x y & rest] (not (apply = x y rest))))
(defn <
([x] true)
([x y] (-lt x y))
([x y & rest] (if (-lt x y)
(apply < y rest)
false)))
(defn >
([x] true)
([x y] (-gt x y))
([x y & rest] (if (-gt x y)
(apply > y rest)
false)))
(defn <=
([x] true)
([x y] (-lte x y))
([x y & rest] (if (-lte x y)
(apply <= y rest)
false)))
(defn >=
([x] true)
([x y] (-gte x y))
([x y & rest] (if (-gte x y)
(apply >= y rest)
false)))
(defn pos?
{:doc "Returns true if x is greater than zero."
:signatures [[x]]
:added "0.1"}
[x]
(> x 0))
(defn neg?
{:doc "Returns true if x is less than zero."
:signatures [[x]]
:added "0.1"}
[x]
(< x 0))
(defn zero?
{:doc "Returns true if x is equal to zero."
:signatures [[x]]
:added "0.1"}
[x]
(= x 0))
(defn inc
{:doc "Increments x by one."
:signatures [[x]]
:added "0.1"}
[x]
(+ x 1))
(defn dec
{:doc "Decrements x by one."
:signatures [[x]]
:added "0.1"}
[x]
(- x 1))
(defn unchecked-inc
{:doc "Increments x by one."
:signatures [[x]]
:added "0.1"}
[x]
(unchecked-add x 1))
(defn unchecked-dec
{:doc "Decrements x by one."
:signatures [[x]]
:added "0.1"}
[x]
(unchecked-subtract x 1))
(defn empty?
{:doc "Returns true if the collection has no items, otherwise false."
:signatures [[coll]]
:added "0.1"}
[coll]
(if (satisfies? ICounted coll)
(zero? (count coll))
(not (seq coll))))
(defn not-empty?
{:doc "Returns true if the collection has items, otherwise false."
:signatures [[coll]]
:added "0.1"}
[coll]
(if (seq coll) true false))
(defn even?
{:doc "Returns true if n is even."
:signatures [[n]]
:added "0.1"}
[n]
(zero? (rem n 2)))
(defn odd?
{:doc "Returns true of n is odd."
:signatures [[n]]
:added "0.1"}
[n]
(= (rem n 2) 1))
(defn nth
{:doc "Returns the element at the idx. If the index is not found it will return an error.
However, if you specify a not-found parameter, it will substitute that instead."
:signatures [[coll idx] [coll idx not-found]]
:added "0.1"}
([coll idx] (-nth coll idx))
([coll idx not-found] (-nth-not-found coll idx not-found)))
(defn first
{:doc "Returns the first item in coll, if coll implements IIndexed nth will be used to retrieve
the item from the collection."
:signatures [[coll]]
:added "0.1"}
[coll]
(if (satisfies? IIndexed coll)
(nth coll 0 nil)
(-first coll)))
(defn second
{:doc "Returns the second item in coll, if coll implements IIndexed nth will be used to retrieve
the item from the collection."
:signatures [[coll]]
:added "0.1"}
[coll]
(if (satisfies? IIndexed coll)
(nth coll 1 nil)
(first (next coll))))
(defn third
{:doc "Returns the third item in coll, if coll implements IIndexed nth will be used to retrieve
the item from the collection."
:signatures [[coll]]
:added "0.1"}
[coll]
(if (satisfies? IIndexed coll)
(nth coll 2 nil)
(first (next (next coll)))))
(defn fourth
{:doc "Returns the fourth item in coll, if coll implements IIndexed nth will be used to retrieve
the item from the collection."
:signatures [[coll]]
:added "0.1"}
[coll]
(if (satisfies? IIndexed coll)
(nth coll 3 nil)
(first (next (next (next coll))))))
(defn assoc
{:doc "Associates the key with the value in the collection."
:signatures [[m] [m k v] [m k v & kvs]]
:added "0.1"}
([m] m)
([m k v]
(-assoc m k v))
([m k v & rest]
(apply assoc (-assoc m k v) rest)))
(defn dissoc
{:doc "Removes the value associated with the keys from the collection."
:signatures [[m] [m & ks]]
:added "0.1"}
([m] m)
([m & ks]
(reduce -dissoc m ks)))
(defn contains?
{:doc "Checks if there is a value associated with key in the collection.
Does *not* check for the presence of a value in the collection, only whether
there's a value associated with the key. Use `some` for checking for values."
:examples [["(contains? {:a 1} :a)" nil true]
["(contains? {:a 1} :b)" nil false]
["(contains? #{:a :b :c} :a)" nil true]
["(contains? [:a :b :c] 0)" nil true]
["(contains? [:a :b :c] 4)" nil false]
["(contains? [:a :b :c] :a)" nil false]]
:signatures [[coll key]]
:added "0.1"}
[coll key]
(-contains-key coll key))
(defn hash-set [& args]
{:doc "Creates a hash-set from the arguments of the function."
:added "0.1"}
(set args))
(defn vec
{:doc "Converts a reducable collection into a vector using the (optional) transducer."
:signatures [[coll] [xform coll]]
:added "0.1"}
([coll]
(transduce conj! coll))
([xform coll]
(transduce xform conj! coll)))
(defn get-val [inst]
(get-field inst :val))
(defn comp
{:doc "Composes the given functions, applying the last function first."
:examples [["((comp inc first) [41 2 3])" nil 42]]
:signatures [[f] [f & fs]]
:added "0.1"}
([] identity)
([f] f)
([f1 f2]
(fn [& args]
(f1 (apply f2 args))))
([f1 f2 f3]
(fn [& args]
(f1 (f2 (apply f3 args)))))
([f1 f2 f3 & fs]
(fn [& args]
(apply (transduce comp (apply list f1 f2 f3 fs)) args))))
(defmacro cond
{:doc "Checks if any of the tests is truthy, if so, stops and returns the value of the corresponding body."
:signatures [[] [test then & clauses]]
:added "0.1"}
([] nil)
([test then & clauses]
`(if ~test
~then
(cond ~@clauses))))
(defmacro try [& body]
(loop [catch nil
catch-sym nil
body-items []
finally nil
body (seq body)]
(let [form (first body)]
(if form
(if (not (seq? form))
(recur catch catch-sym (conj body-items form) finally (next body))
(let [head (first form)]
(cond
(= head 'catch) (if catch
(throw [:pixie.stdlib/SyntaxException
"Can only have one catch clause per try"])
(recur (next (next form)) (first (next form)) body-items finally (next body)))
(= head 'finally) (if finally
(throw [:pixie.stdlib/SyntaxException
"Can only have one finally clause per try"])
(recur catch catch-sym body-items (next form) (next body)))
:else (recur catch catch-sym (conj body-items form) finally (next body)))))
(let [catch-data (cond
(keyword? catch-sym) (let [sym (first catch)]
(assert (symbol? sym) (str "Invalid catch binding form"
catch))
[`[(if (= ~catch-sym (ex-data ~sym))
(do ~@(next catch))
(throw ~sym))]
sym])
(seq? catch-sym) (let [sym (first catch)]
(assert (symbol? sym) (str "Invalid catch binding form"
catch))
[`[(if ~catch-sym
(do ~@(next catch))
(throw ~sym))]
sym])
:else [catch catch-sym])]
`(-try-catch
(fn [] ~@body-items)
~(if catch
`(fn [~(nth catch-data 1)] ~@(nth catch-data 0))
`(fn [] nil))
(fn [] ~@finally)))))))
#_(defn .
{:doc "Access the field named by the symbol.
If further arguments are passed, invokes the method named by symbol, passing the object and arguments."
:signatures [[obj sym] [obj sym & args]]
:added "0.1"}
([obj sym]
(get-field obj sym))
([obj sym & args]
(apply (get-field obj sym) obj args)))
(defn true? [v] (identical? v true))
(defn false? [v] (identical? v false))
(defn number? [v] (instance? Number v))
(defn integer? [v] (instance? Integer v))
(defn float? [v] (instance? Float v))
(defn ratio? [v] (instance? Ratio v))
(defn char? [v] (instance? Character v))
(defn string? [v] (instance? String v))
(defn keyword? [v] (instance? Keyword v))
(defn list? [v] (instance? [PersistentList Cons] v))
(defn set? [v] (instance? PersistentHashSet v))
(defn map? [v] (satisfies? IMap v))
(defn fn? [v] (satisfies? IFn v))
(defn coll? [v] (satisfies? IPersistentCollection v))
(defn indexed? [v] (satisfies? IIndexed v))
(defn counted? [v] (satisfies? ICounted v))
(defn map-entry? [v] (satisfies? IMapEntry v))
(defn last
{:doc "Returns the last element of the collection, or nil if none."
:signatures [[coll]]
:added "0.1"}
[coll]
(cond
(satisfies? IIndexed coll)
(when (pos? (count coll))
(nth coll (dec (count coll))))
(satisfies? ISeq coll)
(if (next coll)
(recur (next coll))
(first coll))
(satisfies? ISeqable coll)
(recur (seq coll))))
(defn butlast
{:doc "Returns all elements but the last from the collection."
:signatures [[coll]]
:added "0.1"}
[coll]
(loop [res []
coll coll]
(if (next coll)
(recur (conj res (first coll)) (next coll))
(seq res))))
(defn complement
{:doc "Given a function, returns a new function which takes the same arguments
but returns the opposite truth value."}
[f]
(assert (fn? f) "Complement must be passed a function")
(fn
([] (not (f)))
([x] (not (f x)))
([x y] (not (f x y)))
([x y & more] (not (apply f x y more)))))
(defn constantly [x]
{:doc "Returns a function that always returns x, no matter what it is called with."
:examples [["(let [f (constantly :me)] [(f 1) (f \"foo\") (f :abc) (f nil)])"
nil [:me :me :me :me]]]}
(fn [& _] x))
(extend -count MapEntry (fn [self] 2))
(extend -nth MapEntry (fn map-entry-nth [self idx]
(cond (= idx 0) (-key self)
(= idx 1) (-val self))))
(extend -nth-not-found MapEntry (fn map-entry-nth [self idx not-found]
(cond (= idx 0) (-key self)
(= idx 1) (-val self)
:else not-found)))
(extend -eq MapEntry (fn [self other]
(cond
(map-entry? other) (and (= (-key self)
(-key other))
(= (-val self)
(-val other)))
(vector? other) (= [(-key self) (-val self)]
other)
:else (= (seq self) other))))
(extend -reduce MapEntry indexed-reduce)
(extend -str MapEntry
(fn [v]
(str "[" (transduce (interpose " ") string-builder v) "]")))
(extend -repr MapEntry
(fn [v]
(str "[" (transduce (comp (map -repr) (interpose " ")) string-builder v) "]")))
(extend -hash MapEntry
(fn [v]
(transduce ordered-hash-reducing-fn v)))
(extend -seq MapEntry
(fn [self]
(list (-key self) (-val self))))
(defn select-keys
{:doc "Returns a map containing only the entries of `m` where the entry's key is in `key-seq`."}
[m key-seq]
(with-meta
(transduce
(comp (filter (fn [k] (contains? m k)))
(map (fn [k] [k (get m k)])))
conj {} key-seq)
(meta m)))
(defn keys
{:doc "If called with no arguments, returns a transducer that will extract the key from each map entry. If passed
a collection, will assume that it is a hashmap and return a vector of all keys from the collection."
:signatures [[] [coll]]
:added "0.1"}
([] (map key))
([m]
(transduce (map key) conj! m)))
(defn vals
{:doc "If called with no arguments, returns a transducer that will extract the key from each map entry. If passed
a collection, will assume that it is a hashmap and return a vector of all keys from the collection."
:signatures [[] [coll]]
:added "0.1"}
([] (map val))
([m]
(transduce (map val) conj! m)))