Debian Codegraph @

%3a171/transducers.sls

raw
1 #!r6rs
2
3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 ;; Copyright 2019 Linus Björnstam
5 ;;
6 ;; You may use this code under either the license in the SRFI document or the
7 ;; license below.
8 ;;
9 ;; Permission to use, copy, modify, and/or distribute this software for any
10 ;; purpose with or without fee is hereby granted, provided that the above
11 ;; copyright notice and this permission notice appear in all source copies.
12 ;; The software is provided "as is", without any express or implied warranties.
13 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14
15 (library (srfi :171 transducers)
16 (export rcons reverse-rcons rcount rany revery
17 list-transduce vector-transduce string-transduce bytevector-u8-transduce port-transduce generator-transduce
18 compose
19 tmap tfilter tremove treplace tfilter-map tdrop tdrop-while ttake ttake-while
20 tconcatenate tappend-map tdelete-neighbor-dupes tdelete-duplicates tflatten
21 tsegment tpartition tinterpose tlog tenumerate)
22 (import
23 (except (rnrs)
24 assoc filter find fold-right map partition remove for-each member
25 define-record-type
26 list->vector vector-for-each vector-map vector-fill! vector->list
27 error
28 string-hash string-ci-hash)
29 (srfi :1 lists)
30 (srfi :9 records)
31 (srfi :23 error)
32 (srfi :43 vectors)
33 (srfi :69 basic-hash-tables)
34 (srfi :171 meta))
35
36 ;; A special value to be used as a placeholder where no value has been set and #f
37 ;; doesn't cut it. Not exported, and not really needed.
38 (define-record-type <nothing>
39 (make-nothing)
40 nothing?)
41 (define nothing (make-nothing))
42
43 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 ;; Reducing functions meant to be used at the end at the transducing
45 ;; process. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46
47 ;; a transducer-friendly cons with the empty list as identity
48 (define rcons
49 (case-lambda
50 (() '())
51 ((lst) (reverse! lst))
52 ((lst x) (cons x lst))))
53
54 (define reverse-rcons
55 (case-lambda
56 (() '())
57 ((lst) lst)
58 ((lst x) (cons x lst))))
59
60 ;; Use this as the f in transduce to count the amount of elements passed through.
61 ;; (transduce (tfilter odd?) tcount (list 1 2 3)) => 2
62 (define rcount
63 (case-lambda
64 (() 0)
65 ((result) result)
66 ((result input)
67 (+ 1 result))))
68
69 ;; These two take a predicate and returns reducing functions that behave like
70 ;; any and every from srfi-1
71 (define (rany pred)
72 (case-lambda
73 (() #f)
74 ((result) result)
75 ((result input)
76 (if (pred input)
77 (reduced input)
78 #f))))
79
80 (define (revery pred)
81 (case-lambda
82 (() #t)
83 ((result) result)
84 ((result input)
85 (if (and result (pred input))
86 input
87 (reduced #f)))))
88
89 (define list-transduce
90 (case-lambda
91 ((xform f coll)
92 (list-transduce xform f (f) coll))
93 ((xform f init coll)
94 (let* ((xf (xform f))
95 (result (list-reduce xf init coll)))
96 (xf result)))))
97
98 (define vector-transduce
99 (case-lambda
100 ((xform f coll)
101 (vector-transduce xform f (f) coll))
102 ((xform f init coll)
103 (let* ((xf (xform f))
104 (result (vector-reduce xf init coll)))
105 (xf result)))))
106
107 (define string-transduce
108 (case-lambda
109 ((xform f coll)
110 (string-transduce xform f (f) coll))
111 ((xform f init coll)
112 (let* ((xf (xform f))
113 (result (string-reduce xf init coll)))
114 (xf result)))))
115
116 (define bytevector-u8-transduce
117 (case-lambda
118 ((xform f coll)
119 (bytevector-u8-transduce xform f (f) coll))
120 ((xform f init coll)
121 (let* ((xf (xform f))
122 (result (bytevector-u8-reduce xf init coll)))
123 (xf result)))))
124
125 (define port-transduce
126 (case-lambda
127 ((xform f by port)
128 (port-transduce xform f (f) by port))
129 ((xform f init by port)
130 (let* ((xf (xform f))
131 (result (port-reduce xf init by port)))
132 (xf result)))))
133
134 (define generator-transduce
135 (case-lambda
136 ((xform f gen)
137 (generator-transduce xform f (f) gen))
138 ((xform f init gen)
139 (let* ((xf (xform f))
140 (result (generator-reduce xf init gen)))
141 (xf result)))))
142
143 ;; compose unary functions
144 (define (compose . fns)
145 (define (make-chain fn chain)
146 (lambda args
147 (call-with-values (lambda () (apply fn args)) chain)))
148 (reduce make-chain values fns))
149
150 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
151 ;; Transducers! ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
152
153 (define (tmap f)
154 (lambda (reducer)
155 (case-lambda
156 (() (reducer))
157 ((result) (reducer result))
158 ((result input)
159 (reducer result (f input))))))
160
161 (define (tfilter pred)
162 (lambda (reducer)
163 (case-lambda
164 (() (reducer))
165 ((result) (reducer result))
166 ((result input)
167 (if (pred input)
168 (reducer result input)
169 result)))))
170
171 (define (tremove pred)
172 (lambda (reducer)
173 (case-lambda
174 (() (reducer))
175 ((result) (reducer result))
176 ((result input)
177 (if (not (pred input))
178 (reducer result input)
179 result)))))
180
181 (define (tfilter-map f)
182 (compose (tmap f) (tfilter values)))
183
184 (define (make-replacer map)
185 (cond
186 ((list? map)
187 (lambda (x)
188 (let ((replacer? (assoc x map)))
189 (if replacer?
190 (cdr replacer?)
191 x))))
192 ((hash-table? map)
193 (lambda (x)
194 (hash-table-ref/default map x x)))))
195
196 (define (treplace map)
197 (tmap (make-replacer map)))
198
199 (define (tdrop n)
200 (lambda (reducer)
201 (let ((new-n (+ 1 n)))
202 (case-lambda
203 (() (reducer))
204 ((result) (reducer result))
205 ((result input)
206 (set! new-n (- new-n 1))
207 (if (positive? new-n)
208 result
209 (reducer result input)))))))
210
211 (define (tdrop-while pred)
212 (lambda (reducer)
213 (let ((drop? #t))
214 (case-lambda
215 (() (reducer))
216 ((result) (reducer result))
217 ((result input)
218 (if (and (pred input) drop?)
219 result
220 (begin
221 (set! drop? #f)
222 (reducer result input))))))))
223
224 (define (ttake n)
225 (lambda (reducer)
226 ;; we need to reset new-n for every new transduction
227 (let ((new-n n))
228 (case-lambda
229 (() (reducer))
230 ((result) (reducer result))
231 ((result input)
232 (let ((result (if (positive? new-n)
233 (reducer result input)
234 result)))
235 (set! new-n (- new-n 1))
236 (if (not (positive? new-n))
237 (ensure-reduced result)
238 result)))))))
239
240 (define ttake-while
241 (case-lambda
242 ((pred) (ttake-while pred (lambda (result input) result)))
243 ((pred retf)
244 (lambda (reducer)
245 (let ((take? #t))
246 (case-lambda
247 (() (reducer))
248 ((result) (reducer result))
249 ((result input)
250 (if (and take? (pred input))
251 (reducer result input)
252 (begin
253 (set! take? #f)
254 (ensure-reduced (retf result input)))))))))))
255
256 (define (tconcatenate reducer)
257 (let ((preserving-reducer (preserving-reduced reducer)))
258 (case-lambda
259 (() (reducer))
260 ((result) (reducer result))
261 ((result input)
262 (list-reduce preserving-reducer result input)))))
263
264 (define (tappend-map f)
265 (compose (tmap f) tconcatenate))
266
267 ;; Flattens everything and passes each value through the reducer
268 ;; (list-transduce tflatten conj (list 1 2 (list 3 4 '(5 6) 7 8))) => (1 2 3 4 5 6 7 8)
269 (define tflatten
270 (lambda (reducer)
271 (case-lambda
272 (() '())
273 ((result) (reducer result))
274 ((result input)
275 (if (list? input)
276 (list-reduce (preserving-reduced (tflatten reducer)) result input)
277 (reducer result input))))))
278
279 ;; removes duplicate consecutive elements
280 (define tdelete-neighbor-dupes
281 (case-lambda
282 (() (tdelete-neighbor-dupes equal?))
283 ((equality-pred?)
284 (lambda (reducer)
285 (let ((prev nothing))
286 (case-lambda
287 (() (reducer))
288 ((result) (reducer result))
289 ((result input)
290 (if (equality-pred? prev input)
291 result
292 (begin
293 (set! prev input)
294 (reducer result input))))))))))
295
296 ;; Deletes all duplicates that passes through.
297 (define tdelete-duplicates
298 (case-lambda
299 (() (tdelete-duplicates equal?))
300 ((equality-pred?)
301 (lambda (reducer)
302 (let ([already-seen (make-hash-table equality-pred?)])
303 (case-lambda
304 [() (reducer)]
305 [(result) (reducer result)]
306 [(result input)
307 (if (hash-table-exists? already-seen input)
308 result
309 (begin
310 (hash-table-set! already-seen input #t)
311 (reducer result input)))]))))))
312
313 ;; Partitions the input into lists of N items. If the input stops it flushes whatever
314 ;; it has collected, which may be shorter than n.
315 ;; I am not sure about the correctness about this. It seems to work.
316 (define (tsegment n)
317 (if (not (and (integer? n) (positive? n)))
318 (error "argument to tsegment must be a positive integer")
319 (lambda (reducer)
320 (let ((i 0)
321 (collect (make-vector n)))
322 (case-lambda
323 (() (reducer))
324 ((result)
325 ;; if there is anything collected when we are asked to quit
326 ;; we flush it to the remaining transducers
327 (let ((result
328 (if (zero? i)
329 result
330 (reducer result (vector->list collect 0 i)))))
331 (set! i 0)
332 ;; now finally, pass it downstreams
333 (reducer result)))
334 ((result input)
335 (vector-set! collect i input)
336 (set! i (+ i 1))
337 ;; If we have collected enough input we can pass it on downstream
338 (if (< i n)
339 result
340 (let ((next-input (vector->list collect 0 i)))
341 (set! i 0)
342 (reducer result next-input)))))))))
343
344 ;; I am not sure about the correctness of this. It seems to work.
345 ;; we could maybe make it faster?
346 (define (tpartition f)
347 (lambda (reducer)
348 (let* ((prev nothing)
349 (collect '()))
350 (case-lambda
351 (() (reducer))
352 ((result)
353 (let ((result
354 (if (null? collect)
355 result
356 (reducer result (reverse! collect)))))
357 (set! collect '())
358 (reducer result)))
359 ((result input)
360 (let ((fout (f input)))
361 (cond
362 ((or (equal? fout prev) (nothing? prev)) ; collect
363 (set! prev fout)
364 (set! collect (cons input collect))
365 result)
366 (else ; flush what we collected already to the reducer
367 (let ((next-input (reverse! collect)))
368 (set! prev fout)
369 (set! collect (list input))
370 (reducer result next-input))))))))))
371
372 ;; Interposes element between each value pushed through the transduction.
373 (define (tinterpose elem)
374 (lambda (reducer)
375 (let ((send-elem? #f))
376 (case-lambda
377 (() (reducer))
378 ((result)
379 (reducer result))
380 ((result input)
381 (if send-elem?
382 (let ((result (reducer result elem)))
383 (if (reduced? result)
384 result
385 (reducer result input)))
386 (begin
387 (set! send-elem? #t)
388 (reducer result input))))))))
389
390 ;; indexes every value passed through in a cons pair as in (index . value). By default starts at 0
391 (define tenumerate
392 (case-lambda
393 (() (tenumerate 0))
394 ((n)
395 (lambda (reducer)
396 (let ((n n))
397 (case-lambda
398 (() (reducer))
399 ((result) (reducer result))
400 ((result input)
401 (let ((input (cons n input)))
402 (set! n (+ n 1))
403 (reducer result input)))))))))
404
405 (define tlog
406 (case-lambda
407 (() (tlog (lambda (result input) (write input) (newline))))
408 ((log-function)
409 (lambda (reducer)
410 (case-lambda
411 (() (reducer))
412 ((result) (reducer result))
413 ((result input)
414 (log-function result input)
415 (reducer result input))))))))