forked from pixie-lang/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.pxi
More file actions
2957 lines (2446 loc) · 73.7 KB
/
bootstrap.pxi
File metadata and controls
2957 lines (2446 loc) · 73.7 KB
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
;; This file is used to build what we need to even start running stdlib.pxi
;; Ordering of stuff will probably matter here
(defprotocol ISeq
(-first [this])
(-next [this]))
(defprotocol ISeqable
(-seq [this]))
(defprotocol ICounted
(-count [this]))
(defprotocol IIndexed
(-nth [this idx])
(-nth-not-found [this idx not-found]))
(defprotocol IPersistentCollection
(-conj [this x])
(-disj [this x]))
(defprotocol IEmpty
(-empty [this]))
(defprotocol IObject
(-hash [this])
(-eq [this other])
(-str [this sbf])
(-repr [this sbf]))
(defprotocol IReduce
(-reduce [this f init]))
(defprotocol IDeref
(-deref [this]))
(defprotocol IReset
(-reset! [this val]))
(defprotocol INamed
(-namespace [this])
(-name [this]))
(defprotocol IAssociative
(-assoc [this k v])
(-contains-key [this k])
(-dissoc [this k]))
(defprotocol ILookup
(-val-at [this]))
(defprotocol IMapEntry
(-key [this])
(-val [this]))
(defprotocol IStack
(-push [this v]))
(defprotocol IPop
(-pop [this]))
(defprotocol IFn
(-invoke [this args]))
(defprotocol IDoc
(-doc [this]))
(defprotocol IVector)
(defprotocol ISequential)
(defprotocol IMap)
(defprotocol IMeta
(-with-meta [this x])
(-meta [this]))
(defprotocol ITransientCollection
(-conj! [this x]))
(defprotocol IToTransient
(-transient [this x]))
(defprotocol ITransient
(-persistent! [this]))
(defprotocol ITransientStack
(-push! [this x])
(-pop! [this]))
(defprotocol IDisposable
(-dispose! [this]))
(defprotocol IMessageObject
(-get-field [this name])
(-invoke-method [this name args]))
(defprotocol IEffect
(-effect-val [this v])
(-effect-finally [this v]))
(defeffect EException
(-throw [this kw data ks]))
(deftype ExceptionHandler [catches finally-fn]
IEffect
(-effect-val [this v]
v)
(-effect-finally [this v]
(when finally-fn
(finally-fn))
v)
EException
(-throw [this kw data ks k]
(let [c (or (get catches kw)
(get catches :*))]
(println "Got Exception" kw c)
(if c
(c {:ex kw :data data :ks (conj ks k)})
(-throw nil kw data (cons k ks))))))
(defn throw
([[kw val]]
(throw kw val))
([kw val]
(-throw nil
kw
val
[])))
(defn -try [body catches finally]
(with-handler [ex (->ExceptionHandler catches finally)]
(body)))
(-run-external-extends)
(extend -get-field Object -internal-get-field)
(extend -hash Object -internal-identity-hash)
(extend -meta Object (fn [x] nil))
(extend-type Object
(-str [x sb]
(sb (-internal-to-str x)))
(-repr [x sb]
(sb (-internal-to-repr x)))
(-eq [this other]
false))
(extend-type String
IObject
(-str [this sb]
(sb this)))
;; Math wrappers
(extend -eq Number -num-eq)
(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 & more]
(-apply + (+ x y) more)))
(defn -
([] 0)
([x] x)
([x y] (-sub x y))
([x y & more]
(-apply - (- x y) more)))
(defn *
([] 1)
([x] x)
([x y] (-mul x y))
([x y & args]
(reduce -mul (-mul 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 inc
([x] (+ x 1)))
(defn dec
([x] (- x 1)))
(defn <
([x y] (-lt x y))
([x y & more]
(-apply < (< x y) more)))
(defn >
([x y] (-gt x y))
([x y & more]
(-apply > (> x y) more)))
(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 =
{: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] (if (identical? x y)
true
(-eq x y)))
([x y & rest] (if (eq 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 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))
;; Base functions
(defn hash
[this]
(-hash this))
(defn identity
^{:doc "The identity function. Returns its argument."
:added "0.1"}
([x & _] x))
(defn not
[x]
(if x false true))
(defn not=
([& args]
(not (-apply = args))))
(defn nil?
[x]
(identical? x nil))
(defn deref
[x]
(-deref x))
(defn conj
{: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"}
([] [])
([coll] coll)
([coll itm] (-conj coll itm))
([coll item & more]
(-apply conj (conj x y) more)))
(defn conj!
{: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"}
([] (-transient pixie.stdlib.persistent-vector/EMPTY))
([coll] (-persistent! coll))
([coll item] (-conj! coll item))
([coll item & args]
(reduce -conj! (-conj! coll item) args)))
(defn disj
{:doc "Removes elements from the collection."
:signatures [[] [coll] [coll item]]
:added "0.1"}
([] [])
([coll] coll)
([coll item] (-disj coll item))
([coll item & items]
(reduce -disj (-disj coll item) items)))
(defn pop
{:doc "Pops elements off a stack."
:signatures [[] [coll] [coll item] [coll item & args]]
:added "0.1"}
([] [])
([coll] (-pop coll)))
(defn push
{:doc "Push an element on to a stack."
:signatures [[] [coll] [coll item] [coll item & args]]
:added "0.1"}
([coll x] (-push coll x)))
(defn pop!
{:doc "Pops elements off a transient stack."
:signatures [[] [coll] [coll item] [coll item & args]]
:added "0.1"}
([coll] (-pop! coll)))
(defn push!
{:doc "Push an element on to a transient stack."
:signatures [[] [coll] [coll item] [coll item & args]]
:added "0.1"}
([coll x] (-push! coll x)))
(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 get
{:doc "Get an element from a collection implementing ILookup, return nil or the default value if not found."
:added "0.1"}
([mp k]
(get mp k nil))
([mp k not-found]
(-val-at mp k not-found)))
(defn get-in
{:doc "Get a value from a nested collection at the \"path\" given by the keys."
:examples [["(get-in {:a [{:b 42}]} [:a 0 :b])" nil 42]]
:signatures [[m ks] [m ks not-found]]
:added "0.1"}
([m ks]
(reduce get m ks))
([m ks not-found]
(loop [sentinel 'x
m m
ks (seq ks)]
(if ks
(let [m (get m (first ks) sentinel)]
(if (identical? sentinel m)
not-found
(recur sentinel m (next ks))))
m))))
(defn contains?
[mp k]
(-contains-key mp k))
(defn name
[x] (-name x))
(defn namespace
[x] (-namespace x))
(defn dispose!
"Finalizes use of the object by cleaning up resources used by the object"
[x]
(-dispose! x)
nil)
(defn has-meta?
[x]
(satisfies? IMeta x))
(defn meta
[x]
(-meta x))
(defn with-meta
[x m]
(-with-meta x m))
(defn count
([coll]
(if (counted? coll)
(-count coll)
(loop [s (seq coll)
i 0]
(if (counted? s)
(+ i (count s))
(recur (next s)
(inc i)))))))
(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 merge
([a] a)
([a b]
(reduce
(fn [a [k v]]
(assoc a k v))
a
b))
([a b & cs]
(apply merge (merge a b) cs)))
(defn assoc-in
{:doc "Associate a value in a nested collection given by the path.
Creates new maps if the keys are not present."
:examples [["(assoc-in {} [:a :b :c] 42)" nil {:a {:b {:c 42}}}]]
:added "0.1"}
([m ks v]
(let [ks (seq ks)
k (first ks)
ks (next ks)]
(if ks
(assoc m k (assoc-in (get m k) ks v))
(assoc m k v)))))
(defn update-in
{:doc "Update a value in a nested collection."
:examples [["(update-in {:a {:b {:c 41}}} [:a :b :c] inc)" nil {:a {:b {:c 42}}}]]
:added "0.1"}
[m ks f & args]
(let [f (fn [m] (apply f m args))
update-inner-f (fn update-inner-f
([m f k]
(assoc m k (f (get m k))))
([m f k & ks]
(assoc m k (apply update-inner-f (get m k) f ks))))]
(apply update-inner-f m f ks)))
(defn key [m]
(-key m))
(defn val [m]
(-val 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]
(with-handler [g (->Generator)]
(transduce (map key) yield g 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]
(with-handler [g (->Generator)]
(transduce (map val) yield g m))))
(defn seq [x]
(-seq x))
(defn first [x]
(if (satisfies? ISeq x)
(-first x)
(let [x (seq x)]
(if (nil? x)
nil
(-first x)))))
(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 next [x]
(if (satisfies? ISeq x)
(seq (-next x))
(let [x (seq x)]
(if (nil? x)
nil
(seq (-next x))))))
(defn nthnext
{:doc "Returns the result of calling next n times on the collection."
:examples [["(nthnext [1 2 3 4 5] 2)" nil (3 4 5)]
["(nthnext [1 2 3 4 5] 7)" nil nil]]
:added "0.1"}
[coll n]
(loop [n n
xs (seq coll)]
(if (and xs (pos? n))
(recur (dec n) (next xs))
xs)))
(defn apply [f & args]
(let [last-itm (last args)
but-last-cnt (dec (count args))
arg-array (make-array (+ but-last-cnt
(count last-itm)))
idx (reduce
(fn [idx itm]
(aset arg-array idx itm)
(inc idx))
but-last-cnt
last-itm)]
(array-copy args 0 arg-array 0 but-last-cnt)
(-apply f arg-array)))
(defn fnil [f else]
(fn [x & args]
(apply f (if (nil? x) else x) args)))
(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))))
(defn last [coll]
(if (vector? coll)
(nth coll (dec (count coll)))
(loop [coll coll]
(if-let [v (next coll)]
(recur v)
(first coll)))))
(defn butlast [coll]
(loop [res []
coll coll]
(if (next coll)
(recur (conj res (first coll))
(next coll))
(seq res))))
(defn ith
{:doc "Returns the ith element of the collection, negative values count from the end.
If an index is out of bounds, will throw an Index out of Range exception.
However, if you specify a not-found parameter, it will substitute that instead"
:signatures [[coll i] [coll idx not-found]]
:added "0.1"}
([coll i]
(when coll
(let [idx (if (neg? i) (+ i (count coll)) i)]
(nth coll idx))))
([coll i not-found]
(when coll
(let [idx (if (neg? i) (+ i (count coll)) i)]
(nth coll idx not-found)))))
(defn take
{:doc "Takes n elements from the collection, or fewer, if not enough."
:added "0.1"}
[n coll]
(when (pos? n)
(when-let [s (seq coll)]
(cons (first s) (take (dec n) (next s))))))
(defn drop
{:doc "Drops n elements from the start of the collection."
:added "0.1"}
[n coll]
(let [s (seq coll)]
(if (and (pos? n) s)
(recur (dec n) (next s))
s)))
(defn repeat
([x]
(cons x (lazy-seq (repeat x))))
([n x]
(take n (repeat x))))
(defn take-while
{:doc "Returns a lazy sequence of successive items from coll while
(pred item) returns true. pred must be free of side-effects.
Returns a transducer when no collection is provided."
:added "0.1"}
([pred]
(fn [rf]
(fn
([] (rf))
([result] (rf result))
([result input]
(if (pred input)
(rf result input)
(reduced result))))))
([pred coll]
(lazy-seq
(when-let [s (seq coll)]
(when (pred (first s))
(cons (first s) (take-while pred (rest s))))))))
(defn drop-while
{:doc "Returns a lazy sequence of the items in coll starting from the
first item for which (pred item) returns logical false. Returns a
stateful transducer when no collection is provided."
:added "0.1"}
([pred]
(fn [rf]
(let [dv (atom true)]
(fn
([] (rf))
([result] (rf result))
([result input]
(let [drop? @dv]
(if drop?
(if (pred input)
result
(do
(reset! dv nil)
(rf result input)))
(rf result input))))))))
([pred coll]
(let [step (fn [pred coll]
(let [s (seq coll)]
(if (and s (pred (first s)))
(recur pred (rest s))
s)))]
(lazy-seq (step pred coll)))))
;; TODO: use a transient map in the future
(defn group-by
{:doc "Groups the collection into a map keyed by the result of applying f on each element. The value at each key is a vector of elements in order of appearance."
:examples [["(group-by even? [1 2 3 4 5])" nil {false [1 3 5] true [2 4]}]
["(group-by (partial apply +) [[1 2 3] [2 4] [1 2]])" nil {6 [[1 2 3] [2 4]] 3 [[1 2]]}]]
:signatures [[f coll]]
:added "0.1"}
[f coll]
(reduce (fn [res elem]
(update-in res [(f elem)] (fnil conj []) elem))
{}
coll))
;; TODO: use a transient map in the future
(defn frequencies
{:doc "Returns a map with distinct elements as keys and the number of occurences as values"
:added "0.1"}
[coll]
(reduce (fn [res elem]
(update-in res [elem] (fnil inc 0)))
{}
coll))
(defn partition
{:doc "Separates the collection into collections of size n, starting at the beginning, with an optional step size.
The last element of the result contains the remaining element, not necessarily of size n if
not enough elements were present."
:examples [["(partition 2 [1 2 3 4 5 6])" nil ((1 2) (3 4) (5 6))]
["(partition 2 [1 2 3 4 5])" nil ((1 2) (3 4) (5))]
["(partition 2 1 [1 2 3 4 5])" nil ((1 2) (2 3) (3 4) (4 5) (5))]]
:signatures [[n coll] [n step coll]]
:added "0.1"}
([n coll] (partition n n coll))
([n step coll]
(when-let [s (seq coll)]
(cons (take n s) (lazy-seq (partition n step (drop step s)))))))
(defn partitionf
{:doc "A generalized version of partition. Instead of taking a constant number of elements,
this function calls f with the remaining collection to determine how many elements to
take."
:examples [["(partitionf first [2 :a, 3 :a :b, 4 :a :b :c])"
nil ((2 :a) (3 :a :b) (4 :a :b :c))]]}
[f coll]
(when-let [s (seq coll)]
(lazy-seq
(let [n (f s)]
(cons (take n s)
(partitionf f (drop n s)))))))
(defn seq-reduce [s f acc]
(let [s (seq s)]
(if (reduced? acc)
@acc
(if (nil? s)
acc
(seq-reduce (next s)
f
(f acc (first s)))))))
;; Some logic functions
(defn complement
{:doc "Given a function, return 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)))))
;;
;; Cons and List
(deftype Cons [head tail meta]
IObject
(-str [this sb]
(sb "(")
(let [not-first (atom false)]
(reduce
(fn [_ x]
(if @not-first
(sb " ")
(reset! not-first true))
(-str x sb))
nil
this))
(sb ")"))
ISeq
(-first [this] head)
(-next [this] tail)
ISeqable
(-seq [this] this)
IMeta
(-meta [this] meta)
(-with-meta [this new-meta]
(->Cons head tail new-meta))
IReduce
(-reduce [this f init]
(seq-reduce this f init))
IIndexed
(-nth [self idx]
(loop [i idx
s (seq self)]
(if (or (= i 0)
(nil? s))
(if (nil? s)
(throw [:pixie.stdlib/OutOfRangeException "Index out of Range"])
(first s))
(recur (dec i)
(next s)))))
(-nth-not-found [self idx not-found]
(loop [i idx
s (seq self)]
(if (or (= i 0)
(nil? s))
(if (nil? s)
not-found
(first s))
(recur (dec i)
(next s))))))
(defn cons [head tail]
(assert (satisfies? ISeqable tail) (str "Can't seq " tail))
(->Cons head tail nil))
(deftype List [head tail cnt hash-val meta]
IObject
(-hash [this]
(if hash-val
hash-val
(let [val (reduce
pixie.stdlib.hashing/ordered-hashing-rf
this)]
(set-field! this :hash-val val)
val)))
(-str [this sb]
(sb "(")
(let [not-first (atom false)]
(reduce
(fn [_ x]
(if @not-first
(sb " ")
(reset! not-first true))
(-str x sb))
nil
this))
(sb ")"))
IIndexed
(-nth [self idx]
(loop [i idx
s (seq self)]
(if (or (= i 0)
(nil? s))
(if (nil? s)
(first s)
(throw [:pixie.stdlib/OutOfRangeException "Index out of Range"]))
(recur (dec i)
(next s)))))
(-nth-not-found [self idx not-found]
(loop [i idx
s (seq self)]
(if (or (= i 0)
(nil? s))
(if (nil? s)
not-found
(first s))
(recur (dec i)
(next s)))))
IReduce
(-reduce [this f init]
(seq-reduce this f init))
ISeq
(-first [this] head)
(-next [this] tail)
ICounted
(-count [this] cnt)
ISeqable
(-seq [this] this)
IMeta
(-meta [this] meta)
(-with-meta [this new-meta]
(->List head tail cnt hash-val new-meta))
IPersistentCollection
(-conj [this val]
(->List val this (inc cnt) nil nil)))
(defn list [& args]
(loop [acc nil
idx (dec (count args))
cnt 1]
(if (>= idx 0)
(recur (->List (nth args idx)
acc
cnt
nil
nil)
(dec idx)
(inc cnt))
acc)))
;; LazySeq start
(in-ns :pixie.stdlib.lazy-seq)
(deftype LazySeq [f s hash-val meta-data]
ISeqable
(-seq [this]
(sval this)
(when (.-s this)
(loop [ls (.-s this)]
(if (instance? LazySeq ls)
(recur (sval ls))
(do (set-field! this :s ls)
(seq (.-s this)))))))
ISeq
(-first [this]
(seq this)
(first (.-s this)))
(-next [this]
(seq this)
(next (.-s this)))
IReduce
(-reduce [this f init]
(seq-reduce this f init))
IMessageObject
(-get-field [this field]
(get-field this field)))
(defn sval [this]
(if (.-f this)
(do (set-field! this :s ((.-f this)))
(set-field! this :f nil)
(.-s this))
(.-s this)))
(in-ns :pixie.stdlib)
(defn lazy-seq* [f]
(pixie.stdlib.lazy-seq/->LazySeq f nil nil nil))
(defeffect EGenerator
(-yield [this val]))
(deftype Generator []
IEffect
(-effect-val [this val]
nil)
(-effect-finally [this val]
val)
EGenerator
(-yield [this val k]
(cons val (lazy-seq (k nil)))))
(defn yield
([g] nil)
([g i]
(-yield g i)
g))