/
generator.ss
297 lines (270 loc) · 10.3 KB
/
generator.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
#lang mzscheme
(require-for-syntax
scheme/contract)
(require scheme/contract
mzlib/etc
(only (file "project.ss") partition/mask)
(only (file "yield.ss") yieldable)
(file "base.ss"))
; There is no doubt that lists are useful structures for representing
; many kinds of data, and that folds and maps are a quick, convenient
; way of performing arbitrary bits of list manipulation.
;
; The main problem with the list/fold/map approach is the number of
; temporary lists generated in the process, which can take up a large
; amount of memory.
;
; Generators are a half-way-house between lists and streams that aim
; to reduce memory overhead when large data sources are involved.
;
; A generator is a stream-like accessor that can be repeatedly called
; to return new values from its source. A special "generator-end" value
; is returned to indicate that the source has been exhausted.
;
; For convenience we write a generator of a type "a" as follows:
;
; (gen-> a) === (-> (U a generator-end))
;
; This library provides convenient ways of:
;
; - producing generators from lists
; - combining generators to form other generators
; (c.f. fold, map and so on)
; - accumulating results from generators
; (e.g. back into lists)
; Variables ------------------------------------
; symbol
(define generator-end (gensym 'generator-end))
; Syntax ---------------------------------------
; (_ flat-contract) -> flat-contract
;
; Expands into a contract that works with values and the generator-end symbol.
(define-syntax gen->
(syntax-rules ()
[(_ expr)
(-> (or/c expr generator-end?))]))
; Core procedures ------------------------------
; any -> boolean
(define (generator-end? item)
(eq? item generator-end))
; (listof (gen-> any)) -> (listof any)
(define (generate-all gens)
(map (lambda (item)
(item))
gens))
; Combinators ----------------------------------
; (a b c ... -> d) (gen-> a) (gen-> b) (gen-> c) ... -> (gen-> d)
;
; The generator equivalent of "map" from SRFI 1.
;
; Given a mapping function "fn" and some sources, creates a generator that returns:
;
; (apply fn sources)
;
; If, in a given iteration, any of the sources return generator-end, the mapping
; function is not called, and the generator simply returns generator-end.
(define (generator-map fn . gens)
(let ([id (gensym)])
(lambda ()
(let ([args (generate-all gens)])
(if (ormap generator-end? args)
generator-end
(apply fn args))))))
; (a b c ... k -> k) k (gen-> a) (gen-> b) (gen-> c) ... -> (gen-> k)
;
; One generator equivalent of "fold" from SRFI 1.
;
; Given an iterator function "it", an initial accumulator and some sources,
; creates a generator that returns:
;
; (apply it (append sources (list accum)).
;
; The result is stored after each iteration and used as the accumulator for the
; next iteration.
;
; If, in a given iteration, any of the sources return generator-end, the iterator
; function is not called, and the generator simply returns generator-end.
(define (generator-fold-map proc accum . gens)
(lambda ()
(let ([args (generate-all gens)])
(if (ormap generator-end? args)
generator-end
(begin
; Update the accumulator...
(set! accum (apply proc (append args (list accum))))
; ...and return it.
accum)))))
; (a -> boolean) (gen-> a) -> (gen-> a)
;
; The generator equivalent of "filter" from SRFI 1.
;
; Given a predicate "pred" and a source, creates a generator that returns
; only those source values for which:
;
; (pred source)
;
; is non-#f. Note that this means that a single call to the generator can result
; in multiple calls to the source.
;
; If, in a given iteration, the source returns generator-end, the iterator
; function is not called, and the generator simply returns generator-end.
(define (generator-filter test gen)
(letrec ([ans (lambda ()
(let ([arg (gen)])
(cond [(generator-end? arg) generator-end]
[(test arg) arg]
[else (ans)])))])
ans))
; (a -> (U any #f)) (gen-> a) -> (gen-> any)
;
; The generator equivalent of "filter-map" from SRFI 1.
;
; Given a predicate "pred" and a source, creates a generator that returns non-#f
; values of:
;
; (pred source)
;
; Note that this means that a single call to the generator can result in
; multiple calls to the source.
;
; If, in a given iteration, the source returns generator-end, the iterator
; function is not called, and the generator simply returns generator-end.
(define (generator-filter-map test gen)
(letrec ([ans (lambda ()
(let ([arg (gen)])
(if (generator-end? arg)
generator-end
(let ([answer (test arg)])
(if answer answer (ans))))))])
ans))
; (gen-> a) -> (gen-> a)
(define generator-remove-duplicates
(let ([empty (gensym)])
(opt-lambda (gen [same? equal?])
(let ([last empty])
(lambda ()
(let loop ([curr (gen)])
(cond [(generator-end? curr) generator-end]
[(same? last curr) (set! last curr)
(loop (gen))]
[else (set! last curr)
curr])))))))
; string (gen-> any) -> (gen-> any)
;
; Creates a generator that mimics its source, but prints generated values
; as it goes.
(define (generator-debug message generate)
(lambda ()
(let ([item (generate)])
(printf "~a ~s~n" message item)
item)))
; Accumulators and list interoperability -------
; (a b c ... -> void) (gen-> a) (gen-> b) (gen-> c) ... -> void
;
; Repeatedly calls source generators, supplying their values to an iterator
; procedure, until one or more returns generator-end.
(define (generator-for-each proc . gens)
(let ([args (generate-all gens)])
(if (ormap generator-end? args)
(void)
(begin (apply proc args)
(apply generator-for-each (cons proc gens))))))
; (a b c ... k -> k) k (gen-> a) (gen-> b) (gen-> c) ... -> k
;
; The "proper" equivalent of "fold" from SRFI 1.
;
; Given an iterator function "it", an initial accumulator and some sources,
; repeatedly does:
;
; (apply it (append sources (list accum))
;
; until one or more of the sources returns generator-end. At this point the
; accumulator is returned.
(define (generator-fold proc accum0 . gens)
(let loop ([accum accum0])
(let ([args (generate-all gens)])
(if (ormap generator-end? args)
accum
(loop (apply proc (append args (list accum))))))))
; (listof a) -> (-> (U a generator-end))
;
; Creates a generator that iterates through the values in data and then
; repeatedly returns end.
(define (list->generator data)
(lambda ()
(if (null? data)
generator-end
(begin0 (car data)
(set! data (cdr data))))))
; (gen-> a) -> (listof a)
;
; A convenient form of generator-fold that collects generated values
; into a list.
(define (generator->list gen)
(reverse (generator-fold cons null gen)))
; Snooze specific (TODO : move to Snooze) ------
; (listof boolean)
; (gen-> (listof a))
; [(a a -> boolean)]
; ->
; (gen-> (append (listof a) (listof (listof a))))
;
; Projects items from the supplied generator according to the rules
; set out in project.ss.
;
; Passes non-list items straight through.
(define generator-project
(opt-lambda (mask generate [same? eq?])
(define (projectable? x)
(or (pair? x) (null? x)))
(define collect-nonkeys
(case-lambda
((next-nonkeys nonkeys-accum)
;(if (andmap not next-nonkeys)
; nonkeys-accum
; (cons next-nonkeys nonkeys-accum))
(cons next-nonkeys nonkeys-accum))
((next-nonkeys)
(collect-nonkeys next-nonkeys null))))
(yieldable yield
(define (yield* last-keys last-nonkeys)
(yield (append last-keys (list (reverse last-nonkeys)))))
(lambda ()
(let*-values ([(last) (generate)]
[(keys0 nonkeys0) (if (list? last)
(partition/mask last mask)
(values #f null))])
(let loop ([last last] [last-keys keys0] [last-nonkeys (collect-nonkeys nonkeys0)] [next (generate)])
(let-values ([(next-keys next-nonkeys)
(if (projectable? next)
(partition/mask next mask)
(values #f null))])
(if (projectable? last)
(if (projectable? next)
(if (andmap same? last-keys next-keys)
(loop next next-keys (collect-nonkeys next-nonkeys last-nonkeys) (generate))
(begin (yield* last-keys last-nonkeys)
(loop next next-keys (collect-nonkeys next-nonkeys) (generate))))
(begin (yield* last-keys last-nonkeys)
(loop next #f null (generate))))
(if (projectable? next)
(begin (yield last)
(loop next next-keys (collect-nonkeys next-nonkeys) (generate)))
(begin (yield last)
(loop next #f null (generate))))))))))))
; Provide statements ---------------------------
(provide gen->
generator-end
generator-end?)
(provide/contract
[generator-map (->* (procedure?) () #:rest (listof procedure?) procedure?)]
[generator-fold-map (->* (procedure? any/c) () #:rest (listof procedure?) procedure?)]
[generator-filter (-> procedure? procedure? procedure?)]
[generator-filter-map (-> procedure? procedure? procedure?)]
[generator-remove-duplicates (->* (procedure?) (procedure?) procedure?)]
[generator-debug (-> string? procedure? procedure?)]
[generator-for-each (->* (procedure?) () #:rest (listof procedure?) any)]
[generator-fold (->* (procedure? any/c) () #:rest (listof procedure?) any)]
[generator->list (-> procedure? (or/c pair? null?))]
[list->generator (-> (or/c pair? null?) procedure?)]
[generator-project (->* ((listof boolean?) procedure?) (procedure?) procedure?)])