forked from ijp/iteratees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiteratees.sls
executable file
·380 lines (322 loc) · 9.73 KB
/
iteratees.sls
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
#!r6rs
;;; iteratees.sls --- Iteratees
;; Copyright (C) 2012 Ian Price <[email protected]>
;; Author: Ian Price <[email protected]>
;; This program is free software, you can redistribute it and/or
;; modify it under the terms of the new-style BSD license.
;; You should have received a copy of the BSD license along with this
;; program. If not, see <http://www.debian.org/misc/bsd.license>.
(library (iteratees)
(export make-chunk
chunk?
chunk-data
stream?
make-done
done?
result
make-cont
cont?
cont-k
iteratee?
return
>>=
peek
head
break
heads
take
drop
skip-to-eof
length
drop-while
is-finished?
identity
->port
->file
->string
->output
enum-eof
enum-string
enum-port
enum-file
>>>
run
run-enumerator
&divergent
make-divergent-condition
divergent-condition?
)
(import (except (rnrs) length)
(srfi :13)
(srfi :8)
(prefix (monad maybe) m:))
;; Stream data type
(define-record-type chunk
(fields data))
(define (stream? s)
(or (chunk? s)
(eof-object? s)))
(define-syntax stream-case
(syntax-rules (chunk eof)
((stream-case stream-expr ((chunk) empty-chunk-case) ((chunk s) chunk-case) ((eof) empty-case))
(let ((stream stream-expr))
(if (chunk? stream)
(let ((s (chunk-data stream)))
(if (string-null? s)
empty-chunk-case
chunk-case))
empty-case)))))
;; Iteratee data type
(define-record-type done
(fields (immutable result result)))
(define-record-type cont
;; k : Stream -> Iteratee + Stream
(fields k))
(define (iteratee? x)
(or (done? x)
(cont? x)))
;; utilities
(define empty-chunk (make-chunk ""))
(define (doneM s)
(values (make-done s) empty-chunk))
(define (contM s)
(values (make-cont s) empty-chunk))
(define (string-break p s)
(define idx (string-index s p))
(if idx
(values (string-take s idx)
(string-drop s idx))
(values s "")))
(define (continue i v)
((cont-k i) v))
(define buffer-size 1024)
(define (string-split-at s n)
(values (string-take n s)
(string-drop n s)))
;; Iteratee Monad Instance
(define return make-done)
(define (>>= m f)
(define (do-case i s)
(if (done? i)
(let ((new-i (f (result i))))
(if (cont? new-i)
(continue new-i s)
(values new-i s)))
(values (>>= i f) s)))
(if (done? m)
(f (result m))
(make-cont
(lambda (x)
(receive (i s) (continue m x)
(do-case i s))))))
;; Iteratees
(define peek
(letrec ((step (lambda (stream)
(stream-case stream
((chunk)
(values peek stream))
((chunk s)
(values (make-done (m:just (string-ref s 0))) stream))
((eof)
(values (make-done (m:nothing)) stream))))))
(make-cont step)))
(define head
(letrec ((step (lambda (stream)
(stream-case stream
((chunk) (values peek stream))
((chunk s)
(values (make-done (m:just (string-ref s 0)))
(make-chunk (string-drop s 1))))
((eof)
(values (make-done (m:nothing)) stream))))))
(make-cont step)))
(define (break pred?)
(define (step before)
(lambda (stream)
(stream-case stream
((chunk) (contM (step before)))
((chunk s)
(receive (prefix suffix) (string-break pred? s)
(if (string-null? suffix)
(contM (step (string-append before prefix)))
(values (make-done (string-append before prefix))
(make-chunk suffix)))))
((eof)
(values (make-done before) stream)))))
(make-cont (step "")))
(define (heads prefix)
(define (loop count cs)
(if (null? cs)
(return count)
(make-cont (step count cs))))
(define (step count cs)
(lambda (stream)
(stream-case stream
((chunk)
(values (loop count cs) stream))
((chunk s)
(if (null? cs)
(values (make-done count) stream)
(if (char=? (car cs) (string-ref s 0))
((step (+ 1 count) (cdr cs))
(make-chunk (string-drop s 1)))
(values (make-done count) stream))))
((eof)
(values (make-done count) stream)))))
(loop 0 (string->list prefix)))
(define (take n iter)
(define (step n k)
(lambda (stream)
(stream-case stream
((chunk)
(contM (step n k)))
((chunk s)
(if (< (string-length s) n)
(receive (i s) (k stream)
(values (take (- n (string-length s)) i)
empty-chunk))
(let*-values (((s1 s2) (string-split-at n s))
((i s*) (k (make-chunk s1))))
(values (make-done i) (make-chunk s2)))))
((eof)
(receive (i s) (k stream)
(values (make-done i) stream))))))
(cond ((and (zero? n) (cont? iter))
(return iter))
((cont? iter)
(make-cont (step n (cont-k iter))))
(else
(>>= (drop n)
(lambda (_) (return iter))))))
(define (drop n)
(define (step stream)
(stream-case stream
((chunk)
(values (make-cont step) stream))
((chunk s)
(if (>= (string-length s) n)
(values (make-done '()) (make-chunk (string-drop s n)))
(values (drop (- n (string-length s))) empty-chunk)))
((eof)
(values (make-done '()) stream))))
(make-cont step))
(define length
(letrec ((step (lambda (n)
(lambda (stream)
(stream-case stream
((chunk)
(values (make-cont (step n)) stream))
((chunk s)
(values (make-cont (step (+ n (string-length s))))
empty-chunk))
((eof)
(values (make-done n) stream)))))))
(make-cont (step 0))))
(define skip-to-eof
(make-cont (lambda (stream)
(if (chunk? stream)
(values skip-to-eof empty-chunk)
(values (make-done '()) stream)))))
(define (drop-while pred?)
(define (step stream)
(stream-case stream
((chunk)
(values (make-cont step) stream))
((chunk s)
(let ((idx (string-index s (lambda (x) (not (pred? x))))))
(if idx
(values (make-done '()) (make-chunk (string-drop s idx)))
(values (make-cont step) empty-chunk))))
((eof)
(values (make-done '()) stream))))
(make-cont step))
(define is-finished?
(>>= peek
(lambda (x)
(return (m:nothing? x)))))
(define identity (return '()))
(define (->port p)
(letrec ((step (lambda (stream)
(if (chunk? stream)
(begin
(display (chunk-data stream) p)
(values (make-cont step) (make-chunk "")))
(begin
(close-port p)
(values (make-done '()) stream))))))
(make-cont step)))
(define (->file file)
(->port (open-file-output-port file)))
(define (->port* p)
;; TODO: decide which of ->port and ->port* is best to export
(letrec ((step (lambda (stream)
(if (chunk? stream)
(begin
(display (chunk-data stream) p)
(values (make-cont step) (make-chunk "")))
(begin
(values (make-done '()) stream))))))
(make-cont step)))
(define (->string)
(receive (p get) (open-string-output-port)
(>>= (->port* p)
(lambda (_)
(define result (get))
(close-output-port p)
(return result)))))
(define (->output)
(->port* (current-output-port)))
;; enumerators
(define (enum-eof iter)
(if (cont? iter)
(receive (iter* stream) (continue iter (eof-object))
iter*)
iter))
(define (enum-string string)
(lambda (iter)
(if (cont? iter)
(receive (iter* stream) (continue iter (make-chunk string))
iter*)
iter)))
(define (enum-port port)
(define (loop iter)
(if (cont? iter)
(let ((str (get-string-n port buffer-size)))
(if (eof-object? str)
iter
(receive (iter s) (continue iter (make-chunk str))
(loop iter))))
iter))
loop)
(define (enum-file file)
(enum-port (open-file-input-port file)))
(define >>>
(let ((join (lambda (g f)
(lambda (x)
(f (g x))))))
(case-lambda
(() (lambda (x) x))
((a) a)
((a b) (join a b))
((a b . c)
(fold-left join a (cons b c))))))
;; running iteratees
(define (run iteratee)
(if (done? iteratee)
(result iteratee)
(let ((iter2 (continue iteratee (eof-object))))
(if (done? iter2)
(result iter2)
(diverges 'run "Iteratee diverges on eof" iteratee)))))
(define (run-enumerator enum iteratee)
(run (enum iteratee)))
(define (diverges who message irritant)
(raise
(condition (make-divergent-condition)
(make-who-condition who)
(make-message-condition message)
(make-irritants-condition irritant))))
(define-condition-type &divergent &error
make-divergent-condition
divergent-condition?)
)