File: iterator.thb

package info (click to toggle)
theme-d 7.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,036 kB
  • sloc: lisp: 9,625; sh: 5,321; makefile: 715; ansic: 477
file content (188 lines) | stat: -rw-r--r-- 5,874 bytes parent folder | download | duplicates (2)
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
;; -*-theme-d-*-

;; Copyright (C) 2016, 2021 Tommi Höynälänmaa
;; Distributed under GNU Lesser General Public License version 3,
;; see file doc/LGPL-3.

(define-body (standard-library iterator)


  (define-param-method end-iter (%source %target)
		       (((consumer (:consumer %source %target))) %target pure)
    (consumer null #t null))


  (define-param-method gen-list (%source %target)
		     (((l (:uniform-list %source))
		       (consumer (:consumer %source %target))
		       (genrest (:iterator-inst %source %target)))
		      %target pure)
    (match-type l
      ((<null>) (genrest consumer))
      ((l2 (:nonempty-uniform-list %source))
       (consumer (car l2) #f
		 (lambda (((consumer (:consumer %source %target)))
			  %target pure)
		   (gen-list (cdr l2) consumer genrest))))))


  (define-param-method get-list-iterator (%source)
		       (((l (:uniform-list %source)))
			(:iterator %source)
			pure)
    (param-lambda (%target)
	(((consumer (:consumer %source %target))) %target pure)
      (gen-list l consumer end-iter)))


  (define-param-method gen-mutable-vector (%source %target)
		       (((v (:mutable-vector %source))
			 (consumer (:consumer %source %target))
			 (genrest (:iterator-inst %source %target)))
			%target pure)
    (letrec ((my-loop (:procedure (<integer> (:consumer %source %target))
				  %target pure)
		      (lambda (((i <integer>)
				(consumer (:consumer %source %target)))
			       %target pure)
			(if (equal? i (mutable-vector-length v))
			    (genrest consumer)
			    (consumer
			     (mutable-vector-ref v i)
			     #f
			     (lambda (((consumer (:consumer %source %target)))
				      %target pure)
			       (my-loop (+ i 1) consumer)))))))
      (my-loop 0 consumer)))


  (define-param-method get-mutable-vector-iterator (%source)
		       (((v (:mutable-vector %source)))
			(:iterator %source)
			pure)
    (param-lambda (%target)
	(((consumer (:consumer %source %target))) %target pure)
      (gen-mutable-vector v consumer end-iter)))


  (define-param-method iter-map1 (%source %component)
		       (((proc (:procedure (%source) %component pure))
			 (iter (:iterator %source)))
			(:uniform-list %component) pure)
    (let ((%target (:uniform-list %component)))
      (letrec ((my-loop
		(:procedure ((:iterator-inst %source %target)) %target pure)
		(lambda (((iter (:iterator-inst %source %target))) %target pure)
		  (iter (lambda (((x1 (:maybe %source))
				  (eof1? <boolean>)
				  (iter (:maybe (:iterator-inst %source %target))))
				 %target pure)
			  (if eof1?
			      null
			      (cons
			       (proc (cast %source x1))
			       (my-loop (cast (:iterator-inst %source %target)
					      iter)))))))))
	(my-loop (param-proc-instance iter %target)))))


  (define-param-method iter-map2 (%source1 %source2 %component)
		       (((proc (:procedure (%source1 %source2) %component pure))
			 (iter1 (:iterator %source1))
			 (iter2 (:iterator %source2)))
			(:uniform-list %component)
			pure)
    (let ((%target (:uniform-list %component)))
      (letrec ((my-loop
		(:procedure ((:iterator-inst %source1 %target)
			     (:iterator-inst %source2 %target))
			    %target pure)
		(lambda (((iter1 (:iterator-inst %source1 %target))
			  (iter2 (:iterator-inst %source2 %target)))
			 %target pure)
		  (iter1 (lambda (((x1 (:maybe %source1))
				   (eof1? <boolean>)
				   (iter1
				    (:maybe (:iterator-inst %source1 %target))))
				  %target pure)
			   (iter2 (lambda (((x2 (:maybe %source2))
					    (eof2? <boolean>)
					    (iter2 (:maybe (:iterator-inst
							    %source2 %target))))
					   %target pure)
				    (if (or eof1? eof2?)
					null
					(cons
					 (proc (cast %source1 x1)
					       (cast %source2 x2))
					 (my-loop
					  (cast
					   (:iterator-inst %source1 %target)
					   iter1)
					  (cast
					   (:iterator-inst %source2 %target)
					   iter2)))))))))))
	(my-loop (param-proc-instance iter1 %target)
		 (param-proc-instance iter2 %target)))))


  (define-param-method iter-every1 (%source)
		       (((proc (:procedure (%source) <boolean> pure))
			 (iter (:iterator %source)))
			<boolean> pure)
    (let ((%target <boolean>))
      (letrec ((my-loop
		(:procedure ((:iterator-inst %source %target)) %target pure)
		(lambda (((iter (:iterator-inst %source %target))) %target pure)
		  (iter (lambda (((x1 (:maybe %source))
				  (eof1? <boolean>)
				  (iter (:maybe
					 (:iterator-inst %source %target))))
				 %target pure)
			  (or eof1?
			      (and
			       (proc (cast %source x1))
			       (my-loop (cast (:iterator-inst %source %target)
					      iter)))))))))
	(my-loop (param-proc-instance iter %target)))))


  (define-param-method iter-every2 (%source1 %source2)
		       (((proc (:procedure (%source1 %source2) <boolean> pure))
			 (iter1 (:iterator %source1))
			 (iter2 (:iterator %source2)))
			<boolean>
			pure)
    (let ((%target <boolean>))
      (letrec ((my-loop
		(:procedure ((:iterator-inst %source1 %target)
			     (:iterator-inst %source2 %target))
			    %target pure)
		(lambda (((iter1 (:iterator-inst %source1 %target))
			  (iter2 (:iterator-inst %source2 %target))) %target
			  pure)
		  (iter1 (lambda (((x1 (:maybe %source1))
				   (eof1? <boolean>)
				   (iter1 (:maybe
					   (:iterator-inst %source1 %target))))
				  %target pure)
			   (iter2 (lambda (((x2 (:maybe %source2))
					    (eof2? <boolean>)
					    (iter2 (:maybe (:iterator-inst
							    %source2 %target))))
					   %target pure)
				    (or (or eof1? eof2?)
					(and
					 (proc (cast %source1 x1)
					       (cast %source2 x2))
					 (my-loop (cast
						   (:iterator-inst
						    %source1
						    %target)
						   iter1)
						  (cast
						   (:iterator-inst
						    %source2 %target)
						   iter2)))))))))))
	(my-loop (param-proc-instance iter1 %target)
		 (param-proc-instance iter2 %target))))))