-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiteration.lisp
315 lines (280 loc) · 7.1 KB
/
iteration.lisp
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
(in-package :arc-compat.internal)
(in-readtable :common-lisp)
(in-suite arc-compat)
;;; Iteration
;; while test [body ...]
(mac while (test &body body)
"Executes body repeatedly while test is true. The test is
evaluated before each execution of body."
(w/uniq =>
`(prog ()
,=> (unless ,test (return))
,@body
(go ,=>))))
;>(let ((x 0)) (while (< x 3) (print x) (++ x)))
;0
;1
;2
;nil
;; until test [body ...]
(mac until (test &body body)
"Executes body repeatedly until test is true. The test is
evaluated before each execution of body. until is the opposite of
while."
(w/uniq =>
`(prog ()
,=> (when ,test (return))
,@body
(go ,=>))))
;>(let ((x 0)) (until (> x 3) (print x) (++ x)))
;0
;1
;2
;3
;
;nil
(mac whilet (var test &body body)
"Executes body repeatedly while test is true. The value of test
is assigned to var on each iteration, for use in the
body. Typically test obtains a new value from some source, and
whilet is used to loop until nil is obtained."
(w/uniq =>
`(block nil
(let ,var nil
(tagbody
,=> (setq ,var ,test)
(unless ,var (return))
,@body
(go ,=>))))))
#|(with-input-from-string (in "abc")
(whilet c (read-char in nil nil)
(princ c)))|#
;
;>(w/instring s "abc" (whilet c (readc s) (prn c)))
;a
;b
;c
;
;nil
;[code] [Macro] whiler var expr endval [body ...]
;Executes body repeatedly while expr is not endval. The value of expr is assigned to var on each iteration.
#| arc.arc.lisp
(mac whiler (var expr endval &body body)
(w/uniq =>
`(block nil
(let ,var nil
(tagbody
,=> (setq ,var ,expr)
(when (eql ,var ,endval) (return))
,@body
(go ,=>))))))|#
#|(with-input-from-string (in "abcdef")
(whiler c (read-char in nil nil) #\d
(princ c)))|#
;>(w/instring s "abcdef" (whiler x (readc s) #\d (prn x)))
;a
;b
;c
;
;nil
;
;[code] [Macro] loop start test update [body ...]
;Executes start, then executes body repeatedly, checking test before each iteration and executing update afterward.
;
;
;>(loop (= x 0) (< x 3) (++ x) (prn x))
;0
;1
;2
;
;nil
;; drain expr [eof]
(mac drain (expr &optional eof)
"Repeatedly executes expr until it returns eof (default nil).
A list of the expr values is returned."
(w/uniq (var acc => ans)
`(block nil
(let ,ans (list () )
(with (,var nil ,acc ,ans)
(tagbody
,=> (setq ,var ,expr)
(when (eql ,var ,eof) (return (cdr ,ans)))
(rplacd ,acc (setq ,acc (list ,var)))
(go ,=>)))))))
#|(with-input-from-string (s "(1 2) (3 4)")
(drain (read s nil)))|#
;
;>(w/instring s "(1 2) (3 4)" (drain (sread s nil)))
;((1 2) (3 4))
;
;>(let x 256 (drain (= x (/ x 2)) 1))
;(128 64 32 16 8 4 2)
;[code] [Macro] for var init max [body ...]
;
(defmacro for (var init max &body body)
"Executes body repeatedly with var assigned the values from init to max,
incremented by 1 each time. For the last iteration, v will be >= max.
If max <= init-1, body will not be executed."
(w/uniq (=> block _max)
`(block ,block
(cl:let ((,var ,init)
(,_max (1+ ,max)))
(tagbody
,=> (cond ((>= ,var ,_max)
(return-from ,block)))
(progn ,@body)
(incf ,var)
(go ,=>))))))
(tst for
(== (with-output-to-string (out)
(for x 2.5 4.0 (princ x out)))
"2.53.54.5")
(== (with-output-to-string (out)
(for x 2.5 1.6 (princ x out)))
"2.5"))
;[code] [Macro] repeat n [body ...]
;Executes body n times. Non-integers are rounded up.
(mac repeat (n &body body)
(w/uniq (var =>)
`(prog ((,var ,n))
(cl:declare (cl:fixnum ,var))
,=> (when (cl:>= 0 ,var) (return))
,@body
(setq ,var (cl:1- ,var))
(go ,=>))))
;>(repeat 3 (print "hi"))
;hi
;hi
;hi
;
;nil
;
;[code] [Macro] forlen var seq [body ...]
(mac forlen (var s . body)
"Iterates over a sequence (list, string, or table) seq. var takes the values
from 0 to length-1."
`(for ,var 0 (- (len ,s) 1) ,@body))
(tst forlen
(== (with-output-to-string (*standard-output*)
(let seq '(1 2 3)
(w/obcall (seq)
(forlen x seq (prn x " " (seq x))))))
"0 1
1 2
2 3
")
(== (with-output-to-string (*standard-output*)
(let seq "abc"
(w/obcall (seq)
(forlen x seq (prn x " " (seq x))))))
"0 a
1 b
2 c
")
(== (with-standard-io-syntax
(with-output-to-string (*standard-output*)
(let seq (obj 0 'val0 1 'val1)
(forlen x seq (prn x " " (gethash x seq))))))
"0 VAL0
1 VAL1
"))
;[code] [Macro] each var expr [body ...]
;Executes body, with var taking on each value from expr, which can be a list, string, or table. For a table, var takes on the values, not the keys.
(mac each (var expr &body body)
(w/uniq (gseq g)
`(let ,gseq ,expr
(if (alist ,gseq)
(funcall
(afn (,g)
(when (acons ,g)
(let ,var (car ,g) ,@body)
(self (cdr ,g))))
,gseq)
(isa ,gseq 'table)
(maptable (fn (,g ,var)
,@body)
,gseq)
(for ,g 0 (- (len ,gseq) 1)
(let ,var (cl:elt ,gseq ,g) ,@body))))))
;
;>(each x '(1 (2 3) 4) (prn x))
;1
;(2 3)
;4
;
;nil
;
;>(each x "abc" (prn x))
;a
;b
;c
;
;nil
;
;>(each x (obj key1 'val1 key2 'val2) (prn x))
;val1
;val2
;
;#hash((key2 . val2) (key1 . val1))
;
;[code] [Macro] noisy-each n var val [body ...]
;Version of each, printing a dot every n iterations.
;
;
;>(noisy-each 3 x "abcdefghijk" (pr x))
;ab.cde.fgh.ijk
;
;nil
;
;[code] [Macro] on var s [body ...]
(mac on (var s . body)
"Iterates the same as each, except the variable index is assigned a count,
starting at 0. For tables, var is assigned nil each iteration, so ontable is
probably more useful."
(if (is var 'index)
(err "Can't use index as first arg to on.")
(w/uniq gs
`(let ,gs ,s
(forlen index ,gs
(let ,var (ref ,gs index)
,@body))))))
(tst on
(== (with-output-to-string (*standard-output*)
(on x '(1 (2 3) 4) (prn index " " x)))
"0 1
1 (2 3)
2 4
")
(== (with-output-to-string (*standard-output*)
(on x "abc" (prn index " " x)))
"0 a
1 b
2 c
")
(== (with-standard-io-syntax
(with-output-to-string (*standard-output*)
(on x (obj key1 'val1 key2 'val2) (prn index " " x))))
"0 NIL
1 NIL
"))
;[code] [Macro] ontable k v tab [body ...]
;Iterates over the table tab, assigning k and v each key and value.
;
;
;>(ontable k v (obj key1 'val1 key2 'val2) (prn k " " v))
;key1 val1
;key2 val2
;
;#hash((key2 . val2) (key1 . val1)))
(mac accum (accfn . body)
"Executes body. Inside body, each time accfn is called, its argument is pushed
on a list that becomes the return value. Note that the list is in reverse order."
(w/uniq gacc
`(withs (,gacc nil)
(flet ((,accfn (_)
(push _ ,gacc)))
,@body
(rev ,gacc)))))
(tst accum
(== (accum accfn (each x '(1 2 3) (accfn (* x 10))))
'(10 20 30)))