-
Notifications
You must be signed in to change notification settings - Fork 115
/
iter.ss
926 lines (867 loc) · 31.6 KB
/
iter.ss
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
;;; -*- Gerbil -*-
;;; (C) vyzo
;;; Iterations and comprehensions
(import :gerbil/gambit
:std/error
:std/generic
:std/coroutine
:std/hash-table)
(export
(struct-out iterator)
iter :iter iter-end iter-end? iter-next! iter-fini!
for for* for/collect for/fold
in-iota in-range in-naturals in-hash in-hash-keys in-hash-values
in-input-port in-input-lines in-input-chars in-input-bytes
in-coroutine in-cothread
yield
)
(defstruct iterator (e (next :- :procedure) (fini :- :procedure))
final: #t constructor: :init!)
(defmethod {:init! iterator}
(lambda (self e (next : :procedure) (fini :? :procedure := #f))
(set! self.e e)
(set! self.next next)
(when fini (set! self.fini fini))))
(defstruct :iter-end ())
(def iter-end
(make-:iter-end))
(def (iter-end? obj)
(eq? iter-end obj))
(def (iter obj)
(if (iterator? obj) obj
(:iter obj)))
(defgeneric :iter
(lambda (obj) {:iter obj}))
(defmethod (:iter (it iterator))
it)
(defmethod (:iter (obj :list))
(iter-list obj))
(defmethod (:iter (obj :vector))
(iter-vector obj))
(defmethod (:iter (obj :string))
(iter-string obj))
(defmethod (:iter (obj :u8vector))
(iter-u8vector obj))
(defmethod (:iter (obj HashTable))
(iter-hash-table obj))
(defmethod (:iter (obj :procedure))
(iter-coroutine obj))
(defmethod (:iter (obj :port))
(if (input-port? obj)
(iter-input-port obj)
(raise-bad-argument :iter "input-port" obj)))
(def (iter-list lst)
=> iterator
(def (next it)
(using (it :- iterator)
(match it.e
([hd . rest]
(set! it.e rest)
hd)
(else iter-end))))
(make-iterator lst next))
(defrules defiter-vector ()
((_ iter-vector type length-e ref-e)
(def (iter-vector (vec : type))
=> iterator
(declare (not safe))
(def (next it)
(using (it :- iterator)
(with ([vec . index] it.e)
(if (fx< index (length-e vec))
(let (v (ref-e vec index))
(set! (cdr it.e) (fx1+ index))
v)
iter-end))))
(make-iterator (cons vec 0) next))))
(defiter-vector iter-vector :vector ##vector-length ##vector-ref)
(defiter-vector iter-string :string ##string-length ##string-ref)
(defiter-vector iter-u8vector :u8vector ##u8vector-length ##u8vector-ref)
(def (iter-hash-table (ht : HashTable))
=> iterator
(def (iterate)
(hash-for-each yield ht))
(iter-coroutine iterate))
(def (iter-coroutine (proc : :procedure))
=> iterator
(def (next it)
(using (it :- iterator)
(continue it.e)))
(let (cort (coroutine (lambda () (proc) iter-end)))
(make-iterator cort next)))
(def (iter-cothread (proc : :procedure))
=> iterator
(def (next it)
(using (it :- iterator)
(continue it.e)))
(def (fini it)
(using (it :- iterator)
(when it.e
(cothread-stop! it.e)
(set! it.e #f))))
(let* ((cothr (cothread (lambda () (proc) iter-end)))
(it (make-iterator cothr next fini)))
(make-will it fini)
it))
(def (iter-input-port (port :- :port)
(read-e :- :procedure := read))
=> iterator
(declare (not safe))
(def (next it)
(using (it :- iterator)
(let (val (read-e it.e))
(if (eof-object? val)
iter-end
val))))
(make-iterator port next))
(def (iter-in-iota (start : :number) (count : :fixnum) (step : :number))
=> iterator
(declare (not safe))
(def (next it)
(using (it :- iterator)
(with ([value . limit] it.e)
(using (limit :- :fixnum)
(if (fx> limit 0)
(begin
(set! (car it.e) (+ value step))
(set! (cdr it.e) (fx1- limit))
value)
iter-end)))))
(make-iterator (cons start count) next))
(def* in-iota
((count) (iter-in-iota 0 count 1))
((count start) (iter-in-iota start count 1))
((count start step) (iter-in-iota start count step)))
(defrules defiter-in-range ()
((_ iter-in-range cmp)
(def (iter-in-range (start : :real) (end : :real) (step : :real))
=> iterator
(declare (not safe))
(def (next it)
(using (it :- iterator)
(if (cmp it.e end)
(let (e it.e)
(set! it.e (+ e step))
e)
iter-end)))
(make-iterator start next))))
(defiter-in-range iter-in-range< <)
(defiter-in-range iter-in-range> >)
(def* in-range
((end) (iter-in-range< 0 end 1))
((start end)
(if (> start end)
(iter-in-range> start end -1)
(iter-in-range< start end 1)))
((start end step)
(if (negative? step)
(iter-in-range> start end step)
(iter-in-range< start end step))))
(def (in-naturals (start : :integer := 0) (step : :integer := 1))
=> iterator
(declare (not safe))
(def (next it)
(using (it :- iterator)
(let (value it.e)
(let (value+step (+ value step))
(set! it.e value+step)
value))))
(make-iterator start next))
(def (in-hash (ht : HashTable))
=> iterator
(iter-hash-table ht))
(def (in-hash-keys (ht : HashTable))
=> iterator
(def (iterate)
(hash-for-each (lambda (k v) (yield k)) ht))
(iter-coroutine iterate))
(def (in-hash-values (ht : HashTable))
=> iterator
(def (iterate)
(hash-for-each (lambda (k v) (yield v)) ht))
(iter-coroutine iterate))
(def (in-input-port (obj :~ input-port? :- :port)
(read-e : :procedure := read))
=> iterator
(iter-input-port obj read-e))
(def (in-input-lines (obj :~ input-port? :- :port))
=> iterator
(iter-input-port obj read-line))
(def (in-input-chars (obj :~ input-port? :- :port))
=> iterator
(iter-input-port obj read-char))
(def (in-input-bytes (obj :~ input-port? :- :port))
=> iterator
(iter-input-port obj read-u8))
(def (in-coroutine (proc : :procedure) . args)
=> iterator
(iter-coroutine (if (null? args) proc (cut apply proc args))))
(def (in-cothread (proc : :procedure) . args)
=> iterator
(iter-cothread (if (null? args) proc (cut apply proc args))))
(def (iter-next! (it : iterator))
(it.next it))
(def (iter-fini! (it : iterator))
(@iter-fini! it))
(defrules @iter-fini! ()
((_ it)
(cond
((&iterator-fini it) => (cut <> it)))))
(def (iter-filter (pred : :procedure) iterable)
=> iterator
(def (iterate)
(for (val (iter iterable))
(when (pred val)
(yield val))))
(iter-coroutine iterate))
(begin-syntax
(def (for-binding? bind)
(syntax-case bind (when unless)
((pat expr) (match-pattern? #'pat))
((pat expr when filter-expr) (match-pattern? #'pat))
((pat expr unless filter-expr) (match-pattern? #'pat))
(_ #f)))
(def (for-binding-expr binding)
(syntax-case binding (when unless)
((bind bind-e) #'bind-e)
((bind bind-e when filter-e)
#'(iter-filter (match <> (bind filter-e)) bind-e))
((bind bind-e unless filter-e)
#'(iter-filter (match <> (bind (not filter-e))) bind-e))))
(def (for-binding-bind binding)
(syntax-case binding ()
((bind bind-e . _) #'bind)))
(def (for-iota-args iter-e)
(syntax-case iter-e ()
((_ n) #'(n 0 1))
((_ n start) #'(n start 1))
((_ n start step) #'(n start step))))
(def (for-range-args iter-e)
(syntax-case iter-e ()
((_ end) #'(0 end 1))
((_ start end) #'(start end (if (> $start $end) -1 1)))
((_ start end step) #'(start end step))))
(def (for-naturals-args iter-e)
(syntax-case iter-e ()
((_) #'(0 1))
((_ start) #'(start 1))
((_ start step) #'(start step)))))
(defsyntax (for stx)
(def (generate-for bindings filter body)
(if (fx= (length bindings) 1)
(generate-for1 (car bindings) filter body)
(generate-for* bindings filter body)))
(def (generate-for1 bind filter body)
(let ((iter-e (for-binding-expr bind))
(bind-e (for-binding-bind bind)))
(syntax-case iter-e (in-iota in-range in-naturals)
((in-iota . _)
(generate-for1-iota iter-e bind-e filter body))
((in-range . _)
(generate-for1-range iter-e bind-e filter body))
((in-naturals . _)
(generate-for1-naturals iter-e bind-e filter body))
(_
(with-syntax*
((iter-e iter-e)
(bind-e bind-e)
((body ...) body)
(iter-do-e
(if filter
(with-syntax ((filter-e filter))
#'(lambda (val)
(with ((bind-e val))
(when filter-e
body ...))))
#'(lambda (val)
(with ((bind-e val))
body ...)))))
#'(let ((iterable iter-e)
(iter-do iter-do-e))
(cond
;; speculatively inline list iteration
((pair? iterable)
(for-each iter-do (:- iterable :list)))
((null? iterable) (void))
(else
;; full iteration protocol
(let* ((it (iter iterable))
(next! (&iterator-next it)))
(declare (not safe))
(let lp ()
(let (val (next! it))
(unless (eq? iter-end val)
(iter-do val)
(lp))))
(@iter-fini! it)
(void))))))))))
(def (generate-for1-iota iter-e bind-e filter body)
(with-syntax* (((count start step) (for-iota-args iter-e))
(bind-e bind-e)
((body ...) body)
(iter-do-e
(if filter
(with-syntax ((filter-e filter))
#'(lambda (n)
(let ((bind-e n))
(when filter-e
body ...))))
#'(lambda (n)
(let ((bind-e n))
body ...)))))
#'(let ((iter-do iter-do-e)
($count count)
($start start)
($step step))
(let lp ((i 0) (val $start))
(when (fx< i $count)
(iter-do val)
(lp (fx1+ i) (+ val $step)))))))
(def (generate-for1-range iter-e bind-e filter body)
(with-syntax* (((start end step) (for-range-args iter-e))
(bind-e bind-e)
((body ...) body)
(iter-do-e
(if filter
(with-syntax ((filter-e filter))
#'(lambda (n)
(let ((bind-e n))
(when filter-e
body ...))))
#'(lambda (n)
(let ((bind-e n))
body ...)))))
#'(let* ((iter-do iter-do-e)
($start start)
($end end)
($step step))
(if (negative? $step)
(let lp ((val $start))
(when (> val $end)
(iter-do val)
(lp (+ val $step))))
(let lp ((val $start))
(when (< val $end)
(iter-do val)
(lp (+ val $step))))))))
(def (generate-for1-naturals iter-e bind-e filter body)
(with-syntax* (((start step) (for-naturals-args iter-e))
(bind-e bind-e)
((body ...) body)
(iter-do-e
(if filter
(with-syntax ((filter-e filter))
#'(lambda (n)
(let ((bind-e n))
(when filter-e
body ...))))
#'(lambda (n)
(let ((bind-e n))
body ...)))))
#'(let ((iter-do iter-do-e)
($start start)
($step step))
(let lp ((val $start))
(iter-do val)
(lp (+ val $step))))))
(def (generate-for* bindings filter body)
(with-syntax*
(((it ...)
(gentemps bindings))
((next! ...)
(gentemps bindings))
((iter-e ...)
(stx-map for-binding-expr bindings))
((bind-id ...)
(gentemps bindings))
((bind-e ...)
(stx-map for-binding-bind bindings))
((body ...) body)
(iter-do-e
(if filter
(with-syntax ((filter-e filter))
#'(lambda (bind-id ...)
(with ((bind-e bind-id) ...)
(when filter-e
body ...))))
#'(lambda (bind-id ...)
(with ((bind-e bind-id) ...)
body ...)))))
#'(let* ((it (iter iter-e)) ...
(next! (&iterator-next it)) ...
(iter-do iter-do-e))
(declare (not safe))
(let lp ()
(let ((bind-id (next! it)) ...)
(unless (or (eq? iter-end bind-id) ...)
(iter-do bind-id ...)
(lp))))
(@iter-fini! it) ...
(void))))
(syntax-case stx (when unless)
((_ bind body ...)
(for-binding? #'bind)
(generate-for [#'bind] #f #'(body ...)))
((_ (bind ...) body ...)
(stx-andmap for-binding? #'(bind ...))
(generate-for #'(bind ...) #f #'(body ...)))
((_ (bind ... when expr) body ...)
(stx-andmap for-binding? #'(bind ...))
(generate-for #'(bind ...) #'expr #'(body ...)))
((_ (bind ... unless expr) body ...)
(stx-andmap for-binding? #'(bind ...))
(generate-for #'(bind ...) #'(not expr) #'(body ...)))))
(defrules for* (when unless)
((recur (bind when expr . rest) body ...)
(for-binding? #'bind)
(for (bind when expr) (recur rest body ...)))
((recur (bind unless expr . rest) body ...)
(for-binding? #'bind)
(for (bind unless expr) (recur rest body ...)))
((recur (bind . rest) body ...)
(for-binding? #'bind)
(for (bind) (recur rest body ...)))
((_ () body ...)
(begin body ...)))
(defsyntax (for/collect stx)
(def (generate-for bindings filter body)
(if (fx= (length bindings) 1)
(generate-for1 (car bindings) filter body)
(generate-for* bindings filter body)))
(def (generate-for1 bind filter body)
(let ((iter-e (for-binding-expr bind))
(bind-e (for-binding-bind bind)))
(syntax-case iter-e (in-iota in-range)
((in-iota . _)
(generate-for1-iota iter-e bind-e filter body))
((in-range . _)
(generate-for1-range iter-e bind-e filter body))
(_
(with-syntax
((iter-e iter-e)
(bind-e bind-e)
((body ...) body))
(if filter
(with-syntax ((filter-e filter))
#'(let ((iterable iter-e)
(iter-do
(lambda (val)
(with ((bind-e val))
body ...)))
(iter-test
(lambda (val)
(with ((bind-e val))
filter-e))))
;; full iteration protocol
(let* ((it (iter iterable))
(next! (&iterator-next it)))
(declare (not safe))
(let (root [#f])
(let lp ((tl root))
(let (val (next! it))
(if (eq? iter-end val)
(begin
(@iter-fini! it)
(cdr root))
(if (iter-test val)
(let* ((xval (iter-do val))
(tl* [xval]))
(set! (cdr tl) tl*)
(lp tl*))
(lp tl)))))))))
#'(let ((iterable iter-e)
(iter-do
(lambda (val)
(with ((bind-e val))
body ...))))
(cond
;; speculatively inline list iteration
((pair? iterable)
(map iter-do (:- iterable :list)))
((null? iterable) [])
(else
;; full iteration protocol
(let* ((it (iter iterable))
(next! (&iterator-next it)))
(declare (not safe))
(let (root [#f])
(let lp ((tl root))
(let (val (next! it))
(if (eq? iter-end val)
(begin
(@iter-fini! it)
(cdr root))
(let* ((xval (iter-do val))
(tl* [xval]))
(set! (cdr tl) tl*)
(lp tl*))))))))))))))))
(def (generate-for1-iota iter-e bind-e filter body)
(with-syntax (((count start step) (for-iota-args iter-e))
(bind-e bind-e)
((body ...) body))
(if filter
(with-syntax ((filter-e filter))
#'(let ((iter-do (lambda (n) (let ((bind-e n)) body ...)))
(iter-test (lambda (n) (let ((bind-e n)) filter-e)))
($count count)
($start start)
($step step))
(let (root [#f])
(let lp ((i 0) (val $start) (tl root))
(if (fx< i $count)
(lp (##fx+ i 1)
(+ val $step)
(if (iter-test val)
(let (tl* [(iter-do val)])
(##set-cdr! tl tl*)
tl*)
tl))
(##cdr root))))))
#'(let ((iter-do (lambda (n) (let ((bind-e n)) body ...)))
($count count)
($start start)
($step step))
(let (root [#f])
(let lp ((i 0) (val $start) (tl root))
(if (fx< i $count)
(lp (##fx+ i 1)
(+ val $step)
(let (tl* [(iter-do val)])
(##set-cdr! tl tl*)
tl*))
(##cdr root))))))))
(def (generate-for1-range iter-e bind-e filter body)
(with-syntax (((start end step) (for-range-args iter-e))
(bind-e bind-e)
((body ...) body))
(if filter
(with-syntax ((filter-e filter))
#'(let* ((iter-do (lambda (n) (let ((bind-e n)) body ...)))
(iter-test (lambda (n) (let ((bind-e n)) filter-e)))
($start start)
($end end)
($step step))
(let (root [#f])
(if (negative? $step)
(let lp ((val $start) (tl root))
(if (> val $end)
(lp (+ val $step)
(if (iter-test val)
(let (tl* [(iter-do val)])
(##set-cdr! tl tl*)
tl*)
tl))
(##cdr root)))
(let lp ((val $start) (tl root))
(if (< val $end)
(lp (+ val $step)
(if (iter-test val)
(let (tl* [(iter-do val)])
(##set-cdr! tl tl*)
tl*)
tl))
(##cdr root)))))))
#'(let* ((iter-do (lambda (n) (let ((bind-e n)) body ...)))
($start start)
($end end)
($step step))
(let (root [#f])
(if (negative? $step)
(let lp ((val $start) (tl root))
(if (> val $end)
(lp (+ val $step)
(let (tl* [(iter-do val)])
(##set-cdr! tl tl*)
tl*))
(##cdr root)))
(let lp ((val $start) (tl root))
(if (< val $end)
(lp (+ val $step)
(let (tl* [(iter-do val)])
(##set-cdr! tl tl*)
tl*))
(##cdr root)))))))))
(def (generate-for* bindings filter body)
(with-syntax
((value (genident 'value))
(rvalue (genident 'rvalue))
((it ...)
(gentemps bindings))
((next! ...)
(gentemps bindings))
((iter-e ...)
(stx-map for-binding-expr bindings))
((bind-id ...)
(gentemps bindings))
((bind-e ...)
(stx-map for-binding-bind bindings))
((body ...) body))
(if filter
(with-syntax ((filter-e filter))
#'(let* ((it (iter iter-e)) ...
(next! (&iterator-next it)) ...
(iter-do
(lambda (bind-id ...)
(with ((bind-e bind-id) ...)
body ...)))
(iter-test
(lambda (bind-id ...)
(with ((bind-e bind-id) ...)
filter-e))))
(declare (not safe))
(let (root [#f])
(let lp ((tl root))
(let ((bind-id (next! it)) ...)
(if (or (eq? iter-end bind-id) ...)
(begin (@iter-fini! it) ...
(cdr root))
(if (iter-test bind-id ...)
(let* ((value (iter-do bind-id ...))
(tl* [value]))
(set! (cdr tl) tl*)
(lp tl*))
(lp tl))))))))
#'(let* ((it (iter iter-e)) ...
(next! (&iterator-next it)) ...
(iter-do
(lambda (bind-id ...)
(with ((bind-e bind-id) ...)
body ...))))
(declare (not safe))
(let (root [#f])
(let lp ((tl root))
(let ((bind-id (next! it)) ...)
(if (or (eq? iter-end bind-id) ...)
(begin (@iter-fini! it) ...
(cdr root))
(let* ((value (iter-do bind-id ...))
(tl* [value]))
(set! (cdr tl) tl*)
(lp tl*))))))))))
(syntax-case stx (when unless)
((_ bind body ...)
(for-binding? #'bind)
(generate-for [#'bind] #f #'(body ...)))
((_ (bind ...) body ...)
(stx-andmap for-binding? #'(bind ...))
(generate-for #'(bind ...) #f #'(body ...)))
((_ (bind ... when expr) body ...)
(stx-andmap for-binding? #'(bind ...))
(generate-for #'(bind ...) #'expr #'(body ...)))
((_ (bind ... unless expr) body ...)
(stx-andmap for-binding? #'(bind ...))
(generate-for #'(bind ...) #'(not expr) #'(body ...)))))
(defsyntax (for/fold stx)
(def (for/fold-bind? bind)
(syntax-case bind ()
((id expr) (identifier? #'id))
(else #f)))
(def (generate-for fold-bind bindings filter body)
(if (fx= (length bindings) 1)
(generate-for1 fold-bind (car bindings) filter body)
(generate-for* fold-bind bindings filter body)))
(def (generate-for1 fold-bind bind filter body)
(let ((iter-e (for-binding-expr bind))
(bind-e (for-binding-bind bind)))
(syntax-case iter-e (in-iota in-range)
((in-iota . _)
(generate-for1-iota iter-e bind-e fold-bind filter body))
((in-range . _)
(generate-for1-range iter-e bind-e fold-bind filter body))
(_
(with-syntax
((iter-e iter-e)
(bind-e bind-e)
((fold-iv fold-e) fold-bind)
((body ...) body))
(if filter
(with-syntax ((filter-e filter))
#'(let ((iterable iter-e)
(iter-do
(lambda (val fold-iv)
(with ((bind-e val))
body ...)))
(iter-test
(lambda (val fold-iv)
(with ((bind-e val))
filter-e)))
(fold-iv fold-e))
;; full iteration protocol
(let* ((it (iter iterable))
(next! (&iterator-next it)))
(declare (not safe))
(let lp ((rval fold-e))
(let (val (next! it))
(if (eq? iter-end val)
(begin
(@iter-fini! it)
rval)
(if (iter-test val rval)
(let (xval (iter-do val rval))
(lp xval))
(lp rval))))))))
#'(let ((iterable iter-e)
(iter-do
(lambda (val fold-iv)
(with ((bind-e val))
body ...)))
(fold-iv fold-e))
(cond
;; speculatively inline list iteration
((pair? iterable)
(foldl iter-do fold-iv iterable))
((null? iterable) fold-iv)
(else
;; full iteration protocol
(let* ((it (iter iterable))
(next! (&iterator-next it)))
(declare (not safe))
(let lp ((rval fold-e))
(let (val (next! it))
(if (eq? iter-end val)
(begin
(@iter-fini! it)
rval)
(let (xval (iter-do val rval))
(lp xval)))))))))))))))
(def (generate-for1-iota iter-e bind-e fold-bind filter body)
(with-syntax (((count start step) (for-iota-args iter-e))
(bind-e bind-e)
((fold-iv fold-e) fold-bind)
((body ...) body))
(if filter
(with-syntax ((filter-e filter))
#'(let ((iter-do (lambda (n fold-iv) (let ((bind-e n)) body ...)))
(iter-test (lambda (n fold-iv) (let ((bind-e n)) filter-e)))
($count count)
($start start)
($step step))
(let lp ((i 0) (val $start) (r fold-e))
(if (fx< i $count)
(lp (fx1+ i)
(+ val $step)
(if (iter-test val r)
(iter-do val r)
r))
r))))
#'(let ((iter-do (lambda (n fold-iv) (let ((bind-e n)) body ...)))
($count count)
($start start)
($step step))
(let lp ((i 0) (val $start) (r fold-e))
(if (fx< i $count)
(lp (fx1+ i)
(+ val $step)
(iter-do val r))
r))))))
(def (generate-for1-range iter-e bind-e fold-bind filter body)
(with-syntax (((start end step) (for-range-args iter-e))
(bind-e bind-e)
((fold-iv fold-e) fold-bind)
((body ...) body))
(if filter
(with-syntax ((filter-e filter))
#'(let* ((iter-do (lambda (n fold-iv) (let ((bind-e n)) body ...)))
(iter-test (lambda (n fold-iv) (let ((bind-e n)) filter-e)))
($start start)
($end end)
($step step))
(if (negative? $step)
(let lp ((val $start) (r fold-e))
(if (> val $end)
(lp (+ val $step)
(if (iter-test val r)
(iter-do val r)
r))
r))
(let lp ((val $start) (r fold-e))
(if (< val $end)
(lp (+ val $step)
(if (iter-test val r)
(iter-do val r)
r))
r)))))
#'(let* ((iter-do (lambda (n fold-iv) (let ((bind-e n)) body ...)))
($start start)
($end end)
($step step))
(if (negative? $step)
(let lp ((val $start) (r fold-e))
(if (> val $end)
(lp (+ val $step)
(iter-do val r))
r))
(let lp ((val $start) (r fold-e))
(if (< val $end)
(lp (+ val $step)
(iter-do val r))
r)))))))
(def (generate-for* fold-bind bindings filter body)
(with-syntax
((value (genident 'value))
((loop-id loop-e)
fold-bind)
((it ...)
(gentemps bindings))
((next! ...)
(gentemps bindings))
((iter-e ...)
(stx-map for-binding-expr bindings))
((bind-id ...)
(gentemps bindings))
((bind-e ...)
(stx-map for-binding-bind bindings))
((body ...) body))
(if filter
(with-syntax ((filter-e filter))
#'(let* ((it (iter iter-e)) ...
(next! (&iterator-next it)) ...
(iter-do
(lambda (loop-id bind-id ...)
(with ((bind-e bind-id) ...)
body ...)))
(iter-test
(lambda (loop-id bind-id ...)
(with ((bind-e bind-id) ...)
filter-e))))
(declare (not safe))
(let lp ((loop-id loop-e))
(let ((bind-id (next! it)) ...)
(if (or (eq? iter-end bind-id) ...)
(begin (@iter-fini! it) ...
loop-id)
(if (iter-test loop-id bind-id ...)
(let (value (iter-do loop-id bind-id ...))
(lp value))
(lp loop-id)))))))
#'(let* ((it (iter iter-e)) ...
(next! (&iterator-next it)) ...
(iter-do
(lambda (loop-id bind-id ...)
(with ((bind-e bind-id) ...)
body ...))))
(declare (not safe))
(let lp ((loop-id loop-e))
(let ((bind-id (next! it)) ...)
(if (or (eq? iter-end bind-id) ...)
(begin (@iter-fini! it) ...
loop-id)
(let (value (iter-do loop-id bind-id ...))
(lp value)))))))))
(syntax-case stx (when unless)
((_ fold-bind bind body ...)
(and (for/fold-bind? #'fold-bind)
(for-binding? #'bind))
(generate-for #'fold-bind [#'bind] #f #'(body ...)))
((_ fold-bind (bind ...) body ...)
(and (for/fold-bind? #'fold-bind)
(stx-andmap for-binding? #'(bind ...)))
(generate-for #'fold-bind #'(bind ...) #f #'(body ...)))
((_ fold-bind (bind ... when expr) body ...)
(and (for/fold-bind? #'fold-bind)
(stx-andmap for-binding? #'(bind ...)))
(generate-for #'fold-bind #'(bind ...) #'expr #'(body ...)))
((_ fold-bind (bind ... unless expr) body ...)
(and (for/fold-bind? #'fold-bind)
(stx-andmap for-binding? #'(bind ...)))
(generate-for #'fold-bind #'(bind ...) #'(not expr) #'(body ...)))))