-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjo_clojure_lazy.h
1695 lines (1596 loc) · 62.2 KB
/
jo_clojure_lazy.h
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
#pragma once
// TODO: redo all this stuff using native lambda functions.
// (range)
// (range end)
// (range start end)
// (range start end step)
// Returns a lazy seq of nums from start (inclusive) to end
// (exclusive), by step, where start defaults to 0, step to 1, and end to
// infinity. When step is equal to 0, returns an infinite sequence of
// start. When start is equal to end, returns empty list.
static node_idx_t native_range(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
long long end = args->size(), start = 0, step = 1;
if(end == 0) {
end = LLONG_MAX; // "infinite" series
} else if(end == 1) {
end = get_node(*it++)->as_int();
} else if(end == 2) {
start = get_node(*it++)->as_int();
end = get_node(*it++)->as_int();
} else if(end == 3) {
start = get_node(*it++)->as_int();
end = get_node(*it++)->as_int();
step = get_node(*it++)->as_int();
}
// @ Maybe make 32 configurable
if(end != INT_MAX && (end - start) / step < 32) {
list_ptr_t ret = new_list();
for(long long i = start; i < end; i += step) {
ret->push_back_inplace(new_node_int(i));
}
return new_node_list(ret);
}
// constructs a function which returns the next value in the range, and another function
return new_node_lazy_list(env, new_node_list(list_va(env->get("range-next"), new_node_int(start), new_node_int(step), new_node_int(end))));
}
static node_idx_t native_range_next(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
long long start = get_node(*it++)->as_int();
long long step = get_node(*it++)->as_int();
long long end = get_node(*it++)->as_int();
if(start >= end) {
return NIL_NODE;
}
return new_node_list(list_va(new_node_int(start), env->get("range-next"), new_node_int(start+step), new_node_int(step), new_node_int(end)));
}
// (repeat x)
// (repeat n x)
// Returns a lazy (infinite!, or length n if supplied) sequence of xs.
static node_idx_t native_repeat(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t x;
long long n = LLONG_MAX;
if(args->size() == 1) {
x = eval_node(env, *it++);
} else if(args->size() == 2) {
n = get_node(eval_node(env, *it++))->as_int();
x = eval_node(env, *it++);
} else {
return NIL_NODE;
}
// @ Maybe make 32 configurable
if(n <= 32) {
list_ptr_t ret = new_list();
for(long long i = 0; i < n; i++) {
ret->push_back_inplace(x);
}
return new_node_list(ret);
}
return new_node_lazy_list(env, new_node_list(list_va(env->get("repeat-next"), x, new_node_int(n))));
}
static node_idx_t native_repeat_next(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t x = *it++;
long long n = get_node(*it++)->as_int();
if(n <= 0) {
return NIL_NODE;
}
return new_node_list(list_va(x, env->get("repeat-next"), x, new_node_int(n-1)));
}
// (concat)
// (concat x)
// (concat x y)
// (concat x y & zs)
// Returns a lazy seq representing the concatenation of the elements in the supplied colls.
static node_idx_t native_concat(env_ptr_t env, list_ptr_t args) {
if(args->size() == 0) {
return NIL_NODE;
}
return new_node_lazy_list(env, new_node_list(args->push_front(env->get("concat-next"))));
}
static node_idx_t native_concat_next(env_ptr_t env, list_ptr_t args) {
node_idx_t val = NIL_NODE;
do {
if(args->size() == 0) {
return NIL_NODE;
}
node_idx_t nidx = eval_node(env, args->first_value());
args = args->rest();
node_t *n = get_node(nidx);
auto fr = n->seq_first_rest();
if(fr.third) {
val = fr.first;
args->cons_inplace(fr.second);
break;
}
} while(true);
list_ptr_t ret = new_list();
ret->push_back_inplace(val);
ret->push_back_inplace(env->get("concat-next"));
ret->conj_inplace(*args.ptr);
return new_node_list(ret);
}
// (iterate f x)
// Returns a lazy seq representing the infinite sequence of x, f(x), f(f(x)), etc.
// f must be free of side-effects
static node_idx_t native_iterate(env_ptr_t env, list_ptr_t args) {
return new_node_lazy_list(env, new_node_list(list_va(env->get("iterate-next"), args->first_value(), args->second_value())));
}
static node_idx_t native_iterate_next(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t f = *it++;
node_idx_t x = *it++;
list_ptr_t ret = new_list();
ret->push_back_inplace(x);
ret->push_back_inplace(env->get("iterate-next"));
ret->push_back_inplace(f);
ret->push_back_inplace(eval_va(env, f, x));
return new_node_list(ret);
}
// (map f)
// (map f coll)
// (map f c1 c2)
// (map f c1 c2 c3)
// (map f c1 c2 c3 & colls)
// 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. Returns a transducer when
// no collection is provided.
static node_idx_t native_map(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t f = *it++;
list_ptr_t ret = new_list();
ret->push_back_inplace(env->get("map-next"));
ret->push_back_inplace(f);
while(it) {
ret->push_back_inplace(eval_node(env, *it++));
}
return new_node_lazy_list(env, new_node_list(ret));
}
static node_idx_t native_map_next(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t f = *it++;
// pull off the first element of each list and call f with it
list_ptr_t next_list = new_list();
list_ptr_t arg_list = new_list();
arg_list->push_back_inplace(f);
next_list->push_back_inplace(env->get("map-next"));
next_list->push_back_inplace(f);
for(; it; it++) {
node_idx_t arg_idx = *it;
node_t *arg = get_node(arg_idx);
auto fr = arg->seq_first_rest();
if(!fr.third) {
return NIL_NODE;
}
arg_list->push_back_inplace(fr.first);
next_list->push_back_inplace(fr.second);
}
// call f with the args
node_idx_t ret = eval_list(env, arg_list);
next_list->cons_inplace(ret);
return new_node_list(next_list);
}
// (map-indexed f)(map-indexed f coll)
// Returns a lazy sequence consisting of the result of applying f to 0
// and the first item of coll, followed by applying f to 1 and the second
// item in coll, etc, until coll is exhausted. Thus function f should
// accept 2 arguments, index and item. Returns a stateful transducer when
// no collection is provided.
static node_idx_t native_map_indexed(env_ptr_t env, list_ptr_t args) {
list_ptr_t ret = new_list();
ret->push_back_inplace(env->get("map-indexed-next"));
ret->push_back_inplace(args->first_value());
ret->push_back_inplace(args->second_value());
ret->push_back_inplace(INT_0_NODE);
return new_node_lazy_list(env, new_node_list(ret));
}
static node_idx_t native_map_indexed_next(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t f_idx = *it++;
node_idx_t coll_idx = *it++;
node_idx_t count_idx = *it++;
long long count = get_node(count_idx)->t_int;
// pull off the first element of each list and call f with it
node_t *coll = get_node(coll_idx);
auto fr = coll->seq_first_rest();
if(!fr.third) return NIL_NODE;
list_ptr_t arg_list = new_list();
arg_list->push_back_inplace(f_idx);
arg_list->push_back_inplace(count_idx);
arg_list->push_back_inplace(fr.first);
list_ptr_t next_list = new_list();
next_list->push_back_inplace(env->get("map-indexed-next"));
next_list->push_back_inplace(f_idx);
next_list->push_back_inplace(fr.second);
next_list->push_back_inplace(new_node_int(count + 1));
// call f with the args
node_idx_t ret = eval_list(env, arg_list);
next_list->cons_inplace(ret);
return new_node_list(next_list);
}
// (take n coll)
// Returns a lazy sequence of the first n items in coll, or all items if there are fewer than n.
static node_idx_t native_take(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t n = eval_node(env, *it++);
node_idx_t coll = eval_node(env, *it++);
if(get_node_type(coll) == NODE_LIST) {
// don't do it lazily if not given lazy inputs... thats dumb
long long N = get_node(n)->as_int();
list_ptr_t list_list = get_node(coll)->as_list();
if(list_list->size() <= N) {
return coll;
}
return new_node_list(list_list->take(N));
}
if(get_node_type(coll) == NODE_VECTOR) {
// don't do it lazily if not given lazy inputs... thats dumb
long long N = get_node(n)->as_int();
vector_ptr_t list_list = get_node(coll)->as_vector();
if(list_list->size() <= N) {
return coll;
}
return new_node_vector(list_list->take(N));
}
if(get_node_type(coll) == NODE_LAZY_LIST) {
return new_node_lazy_list(env, new_node_list(list_va(env->get("take-next"), n, coll)));
}
return coll;
}
static node_idx_t native_take_next(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
long long n = get_node(*it++)->as_int();
if(n <= 0) {
return NIL_NODE;
}
node_idx_t coll = *it++;
lazy_list_iterator_t lit(coll);
if(lit.done()) {
return NIL_NODE;
}
return new_node_list(list_va(lit.val, env->get("take-next"), new_node_int(n-1), new_node_lazy_list(lit.env, lit.next_fn())));
}
// (take-nth n) (take-nth n coll)
// Returns a lazy seq of every nth item in coll. Returns a stateful
// transducer when no collection is provided.
static node_idx_t native_take_nth(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t n = eval_node(env, *it++);
if(!it) {
// stateful transducer
node_idx_t lazy_func_idx = new_node(NODE_LIST, 0);
get_node(lazy_func_idx)->t_list = new_list();
get_node(lazy_func_idx)->t_list->push_back_inplace(env->get("take-nth-next"));
get_node(lazy_func_idx)->t_list->push_back_inplace(n);
return new_node_lazy_list(env, lazy_func_idx);
}
long long N = get_node(n)->as_int();
node_idx_t coll = eval_node(env, *it++);
if(get_node_type(coll) == NODE_LIST) {
// don't do it lazily if not given lazy inputs... thats dumb
list_ptr_t list_list = get_node(coll)->as_list();
if(N <= 0) {
// (take-nth 0 coll) will return an infinite sequence repeating for first item from coll. A negative N is treated the same as 0.
node_idx_t lazy_func_idx = new_node(NODE_LIST, 0);
get_node(lazy_func_idx)->t_list = new_list();
get_node(lazy_func_idx)->t_list->push_back_inplace(env->get("constantly-next"));
get_node(lazy_func_idx)->t_list->push_back_inplace(list_list->first_value());
return new_node_lazy_list(env, lazy_func_idx);
}
list_ptr_t list = new_list();
if(list_list->size() <= N) {
list->push_back_inplace(list_list->first_value());
return new_node_list(list);
}
for(list_t::iterator it(list_list); it; it += N) {
list->push_back_inplace(*it);
}
return new_node_list(list);
}
if(get_node_type(coll) == NODE_VECTOR) {
// don't do it lazily if not given lazy inputs... thats dumb
vector_ptr_t list_list = get_node(coll)->as_vector();
if(N <= 0) {
// (take-nth 0 coll) will return an infinite sequence repeating for first item from coll. A negative N is treated the same as 0.
node_idx_t lazy_func_idx = new_node(NODE_LIST, 0);
get_node(lazy_func_idx)->t_list = new_list();
get_node(lazy_func_idx)->t_list->push_back_inplace(env->get("constantly-next"));
get_node(lazy_func_idx)->t_list->push_back_inplace(list_list->first_value());
return new_node_lazy_list(env, lazy_func_idx);
}
vector_ptr_t list = new_vector();
if(list_list->size() <= N) {
list->push_back_inplace(list_list->first_value());
return new_node_vector(list);
}
for(vector_t::iterator it = list_list->begin(); it; it += N) {
list->push_back_inplace(*it);
}
return new_node_vector(list);
}
if(get_node_type(coll) == NODE_LAZY_LIST) {
if(N <= 0) {
lazy_list_iterator_t lit(coll);
// (take-nth 0 coll) will return an infinite sequence repeating for first item from coll. A negative N is treated the same as 0.
node_idx_t lazy_func_idx = new_node(NODE_LIST, 0);
get_node(lazy_func_idx)->t_list = new_list();
get_node(lazy_func_idx)->t_list->push_back_inplace(env->get("constantly-next"));
get_node(lazy_func_idx)->t_list->push_back_inplace(lit.val);
return new_node_lazy_list(lit.env, lazy_func_idx);
}
node_idx_t lazy_func_idx = new_node(NODE_LIST, 0);
get_node(lazy_func_idx)->t_list = new_list();
get_node(lazy_func_idx)->t_list->push_back_inplace(env->get("take-nth-next"));
get_node(lazy_func_idx)->t_list->push_back_inplace(n);
get_node(lazy_func_idx)->t_list->push_back_inplace(coll);
return new_node_lazy_list(env, lazy_func_idx);
}
return coll;
}
static node_idx_t native_take_nth_next(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
long long n = get_node(*it++)->as_int();
if(n <= 0) return NIL_NODE;
node_idx_t coll = *it++;
lazy_list_iterator_t lit(coll);
if(lit.done()) return NIL_NODE;
return new_node_list(list_va(lit.val, env->get("take-nth-next"), new_node_int(n), new_node_lazy_list(lit.env, lit.next_fn(n))));
}
static node_idx_t native_constantly_next(env_ptr_t env, list_ptr_t args) {
node_idx_t value = args->first_value();
return new_node_list(list_va(value, env->get("constantly-next"), value));
}
// (take-last n coll)
// 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.
static node_idx_t native_take_last(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t n = eval_node(env, *it++);
node_idx_t coll = eval_node(env, *it++);
long long N = get_node(n)->as_int();
if(get_node_type(coll) == NODE_LIST) {
list_ptr_t list_list = get_node(coll)->as_list();
if(list_list->size() <= N) {
return coll;
}
return new_node_list(list_list->take_last(N));
}
if(get_node_type(coll) == NODE_VECTOR) {
vector_ptr_t list_list = get_node(coll)->as_vector();
if(list_list->size() <= N) {
return coll;
}
return new_node_vector(list_list->take_last(N));
}
if(get_node_type(coll) == NODE_LAZY_LIST) {
lazy_list_iterator_t lit(coll);
list_ptr_t ret = new_list();
for(; lit; lit.next()) {
node_idx_t value_idx = eval_node(env, lit.val);
node_t *value = get_node(value_idx);
ret->push_back_inplace(value_idx);
if(ret->size() > N) {
ret->pop_front_inplace();
}
}
return new_node_list(ret);
}
return coll;
}
// (distinct)
// (distinct coll)
// Returns a lazy sequence of the elements of coll with duplicates removed.
// Returns a stateful transducer when no collection is provided.
static node_idx_t native_distinct(env_ptr_t env, list_ptr_t args) {
// TODO: lazy? How is that a good idea for distinct? I think it's a bad idea.
list_t::iterator it(args);
node_idx_t node_idx = *it++;
node_t *node = get_node(node_idx);
if(node->is_list()) {
list_ptr_t list_list = node->as_list();
list_ptr_t ret = new_list();
for(list_t::iterator it(list_list); it; it++) {
node_idx_t value_idx = eval_node(env, *it);
node_t *value = get_node(value_idx);
if(!ret->contains([env,value_idx](node_idx_t idx) {
return node_eq(idx, value_idx);
})) {
ret->push_back_inplace(value_idx);
}
}
return new_node_list(ret, NODE_FLAG_LITERAL);
}
if(node->is_vector()) {
vector_ptr_t vector_list = node->as_vector();
vector_ptr_t ret = new_vector();
for(vector_t::iterator it = vector_list->begin(); it; it++) {
node_idx_t value_idx = eval_node(env, *it);
node_t *value = get_node(value_idx);
if(!ret->contains([env,value_idx](node_idx_t idx) {
return node_eq(idx, value_idx);
})) {
ret->push_back_inplace(value_idx);
}
}
return new_node_vector(ret, NODE_FLAG_LITERAL);
}
if(node->is_lazy_list()) {
lazy_list_iterator_t lit(node_idx);
list_ptr_t ret = new_list();
for(; lit; lit.next()) {
node_idx_t value_idx = eval_node(env, lit.val);
node_t *value = get_node(value_idx);
if(!ret->contains([env,value_idx](node_idx_t idx) {
return node_eq(idx, value_idx);
})) {
ret->push_back_inplace(value_idx);
}
}
return new_node_list(ret);
}
return NIL_NODE;
}
// (filter pred)(filter pred coll)
// Returns a lazy sequence of the items in coll for which
// (pred item) returns logical true. pred must be free of side-effects.
// Returns a transducer when no collection is provided.
static node_idx_t native_filter(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t pred_idx = eval_node(env, *it++);
node_idx_t coll_idx = eval_node(env, *it++);
//print_node(coll_idx);
if(get_node_type(coll_idx) == NODE_LIST) {
// don't do it lazily if not given lazy inputs... thats dumb
list_ptr_t list_list = get_node(coll_idx)->as_list();
list_ptr_t ret = new_list();
list_ptr_t args = new_list();
args->push_back_inplace(pred_idx);
for(list_t::iterator it(list_list); it; it++) {
node_idx_t item_idx = *it;//eval_node(env, *it);
//print_node(item_idx);
node_idx_t comp = eval_list(env, args->conj(item_idx));
if(get_node_bool(comp)) {
ret->push_back_inplace(item_idx);
}
}
return new_node_list(ret);
}
if(get_node_type(coll_idx) == NODE_VECTOR) {
vector_ptr_t vector_list = get_node(coll_idx)->as_vector();
vector_ptr_t ret = new_vector();
list_ptr_t args = new_list();
args->push_back_inplace(pred_idx);
for(vector_t::iterator it = vector_list->begin(); it; it++) {
node_idx_t item_idx = *it;//eval_node(env, *it);
//print_node(item_idx);
node_idx_t comp = eval_list(env, args->conj(item_idx));
if(get_node_bool(comp)) {
ret->push_back_inplace(item_idx);
}
}
return new_node_vector(ret);
}
if(get_node_type(coll_idx) == NODE_HASH_MAP) {
// don't do it lazily if not given lazy inputs... thats dumb
hash_map_ptr_t list_list = get_node(coll_idx)->as_hash_map();
hash_map_ptr_t ret = new_hash_map();
list_ptr_t args = new_list();
args->push_back_inplace(pred_idx);
for(hash_map_t::iterator it = list_list->begin(); it; it++) {
list_ptr_t key_val = new_list();
key_val->push_back_inplace(it->first);
key_val->push_back_inplace(it->second);
node_idx_t comp = eval_list(env, args->conj(new_node_list(key_val)));
if(get_node_bool(comp)) {
ret = ret->assoc(it->first, it->second, node_eq);
}
}
return new_node_hash_map(ret);
}
if(get_node_type(coll_idx) == NODE_HASH_SET) {
// don't do it lazily if not given lazy inputs... thats dumb
hash_set_ptr_t list_list = get_node(coll_idx)->as_hash_set();
hash_set_ptr_t ret = new_hash_set();
list_ptr_t args = new_list();
args->push_back_inplace(pred_idx);
for(auto it = list_list->begin(); it; it++) {
node_idx_t comp = eval_list(env, args->conj(it->first));
if(get_node_bool(comp)) {
ret = ret->assoc(it->first, node_eq);
}
}
return new_node_hash_set(ret);
}
if(get_node_type(coll_idx) == NODE_LAZY_LIST) {
return new_node_lazy_list(env, new_node_list(list_va(env->get("filter-next"), pred_idx, coll_idx)));
}
if(get_node_type(coll_idx) == NODE_STRING) {
jo_string str = get_node(coll_idx)->t_string;
jo_string ret;
list_ptr_t args = new_list();
args->push_back_inplace(pred_idx);
size_t str_len = str.length();
const char *str_ptr = str.c_str();
for(long long i = 0; i < str_len; i++) {
node_idx_t item_idx = new_node_int(str_ptr[i], NODE_FLAG_CHAR);
node_idx_t comp = eval_list(env, args->conj(item_idx));
if(get_node_bool(comp)) {
ret += (char)str_ptr[i];
}
}
return new_node_string(ret);
}
return NIL_NODE;
}
static node_idx_t native_filter_next(env_ptr_t env, list_ptr_t args) {
node_idx_t pred_idx = args->first_value();
node_idx_t coll_idx = args->second_value();
list_ptr_t e = new_list();
e->push_back_inplace(pred_idx);
for(lazy_list_iterator_t lit(coll_idx); !lit.done(); lit.next()) {
node_idx_t comp = eval_list(env, e->conj(lit.val));
if(get_node_bool(comp)) {
return new_node_list(list_va(lit.val, env->get("filter-next"), pred_idx, new_node_lazy_list(lit.env, lit.next_fn())));
}
}
return NIL_NODE;
}
// (keep f)(keep f coll)
// Returns a lazy sequence of the non-nil results of (f item). Note,
// this means false return values will be included. f must be free of
// side-effects. Returns a transducer when no collection is provided.
static node_idx_t native_keep(env_ptr_t env, list_ptr_t args) {
node_idx_t f_idx = eval_node(env, args->first_value());
node_idx_t coll_idx = eval_node(env, args->second_value());
return new_node_lazy_list(env, new_node_list(list_va(env->get("keep-next"), f_idx, coll_idx)));
}
static node_idx_t native_keep_next(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t f_idx = *it++;
node_idx_t coll_idx = *it++;
int coll_type = get_node_type(coll_idx);
list_ptr_t e = new_list();
e->push_back_inplace(f_idx);
if(coll_type == NODE_LIST) {
list_ptr_t list_list = get_node(coll_idx)->t_list;
while(!list_list->empty()) {
node_idx_t item_idx = list_list->first_value();
list_list = list_list->pop_front();
node_idx_t comp = eval_list(env, e->conj(item_idx));
if(comp != NIL_NODE) {
return new_node_list(list_va(comp, env->get("keep-next"), f_idx, new_node_list(list_list)));
}
}
}
if(coll_type == NODE_VECTOR) {
vector_ptr_t vec_list = get_node(coll_idx)->as_vector();
while(!vec_list->empty()) {
node_idx_t item_idx = vec_list->first_value();
vec_list = vec_list->pop_front();
node_idx_t comp = eval_list(env, e->conj(item_idx));
if(comp != NIL_NODE) {
return new_node_list(list_va(comp, env->get("keep-next"), f_idx, new_node_vector(vec_list)));
}
}
}
if(coll_type == NODE_LAZY_LIST) {
for(lazy_list_iterator_t lit(coll_idx); !lit.done(); lit.next()) {
node_idx_t item_idx = lit.val;
node_idx_t comp = eval_list(env, e->conj(item_idx));
if(comp != NIL_NODE) {
return new_node_list(list_va(comp, env->get("keep-next"), f_idx, new_node_lazy_list(lit.env, lit.next_fn())));
}
}
}
return NIL_NODE;
}
// (repeatedly f)(repeatedly n f)
// Takes a function of no args, presumably with side effects, and
// returns an infinite (or length n if supplied) lazy sequence of calls
// to it
static node_idx_t native_repeatedly(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t n_idx, f_idx;
if(args->size() == 1) {
n_idx = new_node_int(INT_MAX);
f_idx = eval_node(env, *it++);
} else {
n_idx = eval_node(env, *it++);
f_idx = eval_node(env, *it++);
}
return new_node_lazy_list(env, new_node_list(list_va(env->get("repeatedly-next"), f_idx, n_idx)));
}
static node_idx_t native_repeatedly_next(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t f_idx = *it++;
node_idx_t n_idx = *it++;
long long n = get_node_int(n_idx);
if(n > 0) {
return new_node_list(list_va(eval_va(env, f_idx), env->get("repeatedly-next"), f_idx, new_node_int(n - 1)));
}
return NIL_NODE;
}
// (partition n coll)(partition n step coll)(partition n step pad coll)
// 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.
static node_idx_t native_partition(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t n_idx, step_idx, pad_idx, coll_idx;
if(args->size() == 1) {
return *it;
} else if(args->size() == 2) {
n_idx = *it++;
step_idx = n_idx;
pad_idx = NIL_NODE;
coll_idx = *it++;
} else if(args->size() == 3) {
n_idx = *it++;
step_idx = *it++;
pad_idx = NIL_NODE;
coll_idx = *it++;
} else {
n_idx = *it++;
step_idx = *it++;
pad_idx = *it++;
coll_idx = *it++;
}
return new_node_lazy_list(env, new_node_list(list_va(env->get("partition-next"), n_idx, step_idx, pad_idx, coll_idx)));
}
static node_idx_t native_partition_all(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t n_idx, step_idx, coll_idx;
if(args->size() == 1) {
return *it;
} else if(args->size() == 2) {
n_idx = *it++;
step_idx = n_idx;
coll_idx = *it++;
} else if(args->size() == 3) {
n_idx = *it++;
step_idx = *it++;
coll_idx = *it++;
}
return new_node_lazy_list(env, new_node_list(list_va(env->get("partition-next"), n_idx, step_idx, K_ALL_NODE, coll_idx)));
}
static node_idx_t native_partition_next(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t n_idx = *it++;
node_idx_t step_idx = *it++;
node_idx_t pad_idx = *it++;
node_idx_t coll_idx = *it++;
node_t *coll = get_node(coll_idx);
long long n = get_node_int(n_idx);
long long step = get_node_int(step_idx);
auto t = coll->seq_take(n);
if(!t.second) {
return NIL_NODE;
}
node_idx_t ret_idx = t.first;
node_idx_t new_coll = coll->seq_drop(step);
node_t *ret = get_node(ret_idx);
size_t ret_size = ret->seq_size();
if(ret_size < n) {
if(pad_idx == K_ALL_NODE) {
// let the last one go without pad...
} else if(pad_idx != NIL_NODE) {
long long pad_n = n - ret_size;
seq_iterate(pad_idx, [&](node_idx_t idx) {
ret->seq_push_back(idx);
return --pad_n > 0;
});
} else {
return NIL_NODE;
}
}
return new_node_list(list_va(ret_idx, env->get("partition-next"), n_idx, step_idx, pad_idx, new_coll));
}
// (partition-by f)(partition-by f coll)
// Applies f to each value in coll, splitting it each time f returns a
// new value. Returns a lazy seq of partitions. Returns a stateful
// transducer when no collection is provided.
static node_idx_t native_partition_by(env_ptr_t env, list_ptr_t args) {
node_idx_t f_idx = args->first_value();
node_idx_t coll_idx = args->second_value();
return new_node_lazy_list(env, new_node_list(list_va(env->get("partition-by-next"), f_idx, coll_idx)));
}
static node_idx_t native_partition_by_next(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t f_idx = *it++;
node_idx_t coll_idx = *it++;
node_t *coll = get_node(coll_idx);
auto t = coll->seq_take(1);
if(!t.second) return NIL_NODE;
node_idx_t ret_idx = t.first;
node_t *ret = get_node(ret_idx);
node_idx_t first_idx = eval_va(env, f_idx, ret->seq_first().first);
int num_drop = 0;
seq_iterate(coll_idx, [&](node_idx_t idx) {
node_idx_t pred_idx = eval_va(env, f_idx, idx);
if(!node_eq(pred_idx, first_idx)) {
return false;
}
if(num_drop > 0) {
ret->seq_push_back(idx);
}
++num_drop;
return true;
});
return new_node_list(list_va(ret_idx, env->get("partition-by-next"), f_idx, coll->seq_drop(num_drop)));
}
// (interleave)(interleave c1)(interleave c1 c2)(interleave c1 c2 & colls)
// Returns a lazy seq of the first item in each coll, then the second etc.
// (interleave [:a :b :c] [1 2 3]) => (:a 1 :b 2 :c 3)
static node_idx_t native_interleave(env_ptr_t env, list_ptr_t args) {
if(args->size() == 0) {
return NIL_NODE;
}
list_ptr_t list = new_list();
list->push_back_inplace(env->get("interleave-next"));
list->push_back_inplace(ZERO_NODE);
list->conj_inplace(*args.ptr);
return new_node_lazy_list(env, new_node_list(list));
}
static node_idx_t native_interleave_next(env_ptr_t env, list_ptr_t args) {
if(args->size() == 0) {
return NIL_NODE;
}
node_idx_t coll_idx = args->first_value();
if(coll_idx == ZERO_NODE) {
// check if any of args are done
list_t::iterator it(args);
++it;
for(; it; ++it) {
node_t *n = get_node(*it);
int ntype = n->type;
if(ntype == NODE_NIL) {
return NIL_NODE;
} else if(ntype == NODE_LIST) {
if(n->t_list->size() == 0) {
return NIL_NODE;
}
} else if(ntype == NODE_VECTOR) {
if(n->as_vector()->size() == 0) {
return NIL_NODE;
}
} else if(ntype == NODE_LAZY_LIST) {
if(eval_node(env, n->t_extra) == NIL_NODE) {
return NIL_NODE;
}
} else if(ntype == NODE_STRING) {
if(n->t_string.length() == 0) {
return NIL_NODE;
}
} else {
return NIL_NODE;
}
}
}
node_idx_t nidx = args->second_value();
node_idx_t val = NIL_NODE;
int ntype = get_node(nidx)->type;
list_t::iterator it(args);
args = args->rest(it+2);
if(ntype == NODE_LIST) {
list_ptr_t n = get_node(nidx)->t_list;
val = n->first_value();
args->cons_inplace(new_node_list(n->pop()));
} else if(ntype == NODE_VECTOR) {
vector_ptr_t n = get_node(nidx)->as_vector();
val = n->first_value();
args->cons_inplace(new_node_vector(n->pop_front()));
} else if(ntype == NODE_LAZY_LIST) {
// call the t_extra, and grab the first element of the return and return that.
node_idx_t reti = eval_node(env, get_node(nidx)->t_extra);
node_t *ret = get_node(reti);
if(ret->is_list()) {
list_ptr_t list_list = ret->as_list();
val = list_list->first_value();
args->cons_inplace(new_node_lazy_list(env, new_node_list(list_list->rest())));
}
} else if(ntype == NODE_STRING) {
// pull off the first character of the string
jo_string str = get_node(nidx)->t_string;
val = new_node_string(str.substr(0, 1));
args->cons_inplace(new_node_string(str.substr(1)));
}
list_ptr_t ret = new_list();
ret->push_back_inplace(val);
ret->push_back_inplace(env->get("interleave-next"));
long long next_coll_it = get_node_int(coll_idx)+1;
if(args->size() == next_coll_it) {
ret->push_back_inplace(ZERO_NODE);
} else {
ret->push_back_inplace(new_node_int(next_coll_it));
}
ret->conj_inplace(*args->rest()->clone());
ret->push_back_inplace(args->first_value());
return new_node_list(ret);
}
// (flatten x)
// Takes any nested combination of sequential things (lists, vectors,
// etc.) and returns their contents as a single, flat lazy sequence.
// (flatten nil) returns an empty sequence.
static node_idx_t native_flatten(env_ptr_t env, list_ptr_t args) {
node_idx_t x = args->first_value();
if(x <= NIL_NODE) {
return EMPTY_LIST_NODE;
}
node_t *n = get_node(x);
if(!n->is_seq()) {
return x;
}
node_idx_t lazy_func_idx = new_node(NODE_LIST, 0);
node_t *lazy_func = get_node(lazy_func_idx);
lazy_func->t_list = new_list();
lazy_func->t_list->push_front_inplace(get_node(x)->seq_rest().first);
while(n->is_seq()) {
x = n->seq_first().first;
n = get_node(x);
if(n->is_seq()) {
lazy_func->t_list->push_front_inplace(get_node(x)->seq_rest().first);
} else {
lazy_func->t_list->push_front_inplace(x);
}
}
lazy_func->t_list->push_front_inplace(env->get("flatten-next"));
return new_node_lazy_list(env, lazy_func_idx);
}
static node_idx_t native_flatten_next(env_ptr_t env, list_ptr_t args) {
if(args->size() == 0) {
return NIL_NODE;
}
list_ptr_t ret = new_list();
node_idx_t ret_value = NIL_NODE;
// first value should always not be a sequence
ret_value = args->first_value();
args = args->pop_front();
assert(!get_node(ret_value)->is_seq());
node_idx_t seq_idx = args->first_value();
node_t *seq = get_node(seq_idx);
ret = ret->push_front(args->clone());
if(!seq->is_seq()) {
ret->push_front_inplace(env->get("flatten-next"));
ret->push_front_inplace(ret_value);
return new_node_list(ret);
}
do {
ret = ret->pop();
auto seq_p = seq->seq_first_rest();
if(!get_node(seq_p.second)->seq_empty()) ret->push_front_inplace(seq_p.second);
if(seq_p.first != NIL_NODE) ret->push_front_inplace(seq_p.first);
seq_idx = seq_p.first;
seq = get_node(seq_idx);
} while(seq->is_seq());
ret->push_front_inplace(env->get("flatten-next"));
ret->push_front_inplace(ret_value);
return new_node_list(ret);
}
// (lazy-seq & body)
// Takes a body of expressions that returns an ISeq or nil, and yields
// a Seqable object that will invoke the body only the first time seq
// is called, and will cache the result and return it on all subsequent
// seq calls. See also - realized?
static node_idx_t native_lazy_seq(env_ptr_t env, list_ptr_t args) {
if(args->size() == 0) {
return NIL_NODE;
}
node_idx_t lazy_func_idx = new_node(NODE_LIST, 0);
node_t *lazy_func = get_node(lazy_func_idx);
lazy_func->t_list = args->push_front(new_node_native_function("lazy-seq-first",
[=](env_ptr_t sub_env, list_ptr_t args) -> node_idx_t {
node_idx_t ll_idx = eval_node_list(env, args);
node_t *ll = get_node(ll_idx);
if(!ll->is_seq()) {
return NIL_NODE;
}
auto fr = ll->seq_first_rest();
return new_node_list(list_va(fr.first, env->get("lazy-seq-next"), fr.second));
}, true));
return new_node_lazy_list(env, lazy_func_idx);
}
static node_idx_t native_lazy_seq_next(env_ptr_t env, list_ptr_t args) {
node_idx_t ll_idx = args->first_value();
node_t *ll = get_node(ll_idx);
auto fr = ll->seq_first_rest();
if(!fr.third) return NIL_NODE;
return new_node_list(list_va(fr.first, env->get("lazy-seq-next"), fr.second));
}
// (seq coll)
// Returns a seq on the collection. If the collection is
// empty, returns nil. (seq nil) returns nil. seq also works on
// Strings, native Java arrays (of reference types) and any objects
// that implement Iterable. Note that seqs cache values, thus seq
// should not be used on any Iterable whose iterator repeatedly
// returns the same mutable object.
static node_idx_t native_seq(env_ptr_t env, list_ptr_t args) {
node_idx_t x = args->first_value();
if(x <= NIL_NODE) {
return NIL_NODE;
}
node_t *n = get_node(x);
if(!n->is_seq()) {
return NIL_NODE;
}
return new_node_lazy_list(env, new_node_list(list_va(env->get("seq-next"), x)));
}
static node_idx_t native_seq_next(env_ptr_t env, list_ptr_t args) {
node_idx_t x = args->first_value();
if(x <= NIL_NODE) {
return NIL_NODE;
}
node_t *n = get_node(x);
auto fr = n->seq_first_rest();
if(!fr.third) return NIL_NODE;
return new_node_list(list_va(fr.first, env->get("seq-next"), fr.second));
}
// (cons x seq)
// Returns a new seq where x is the first element and seq is the rest.
// Note: cons is not actually lazy, but I think this implementation
// could benefit from that in the case of cat'ing to a lazy list.
static node_idx_t native_cons(env_ptr_t env, list_ptr_t args) {
list_t::iterator it(args);
node_idx_t first_idx = *it++;
node_idx_t second_idx = *it++;
if(first_idx == NIL_NODE && second_idx == NIL_NODE) {
list_ptr_t ret = new_list();
ret->cons_inplace(NIL_NODE);
return new_node_list(ret);
}
if(first_idx == NIL_NODE) {
list_ptr_t ret = new_list();
ret->cons_inplace(second_idx);