Debian Codegraph @

%3a171/meta.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 meta)
16 (export reduced reduced? unreduce ensure-reduced preserving-reduced
17 list-reduce vector-reduce string-reduce bytevector-u8-reduce port-reduce generator-reduce)
18 (import (except (rnrs) define-record-type)
19 (srfi :9 records))
20
21 ;; A reduced value is stops the transduction.
22 (define-record-type <reduced>
23 (reduced val)
24 reduced?
25 (val unreduce))
26
27 ;; helper function which ensures x is reduced.
28 (define (ensure-reduced x)
29 (if (reduced? x)
30 x
31 (reduced x)))
32
33 ;; helper function that wraps a reduced value twice since reducing functions (like list-reduce)
34 ;; unwraps them. tconcatenate is a good example: it re-uses it's reducer on it's input using list-reduce.
35 ;; If that reduction finishes early and returns a reduced value, list-reduce would "unreduce"
36 ;; that value and try to continue the transducing process.
37 (define (preserving-reduced reducer)
38 (lambda (a b)
39 (let ((return (reducer a b)))
40 (if (reduced? return)
41 (reduced return)
42 return))))
43
44 ;; This is where the magic tofu is cooked
45 (define (list-reduce f identity lst)
46 (if (null? lst)
47 identity
48 (let ((v (f identity (car lst))))
49 (if (reduced? v)
50 (unreduce v)
51 (list-reduce f v (cdr lst))))))
52
53 (define (vector-reduce f identity vec)
54 (let ((len (vector-length vec)))
55 (let loop ((i 0) (acc identity))
56 (if (= i len)
57 acc
58 (let ((acc (f acc (vector-ref vec i))))
59 (if (reduced? acc)
60 (unreduce acc)
61 (loop (+ i 1) acc)))))))
62
63 (define (string-reduce f identity str)
64 (let ((len (string-length str)))
65 (let loop ((i 0) (acc identity))
66 (if (= i len)
67 acc
68 (let ((acc (f acc (string-ref str i))))
69 (if (reduced? acc)
70 (unreduce acc)
71 (loop (+ i 1) acc)))))))
72
73 (define (bytevector-u8-reduce f identity vec)
74 (let ((len (bytevector-length vec)))
75 (let loop ((i 0) (acc identity))
76 (if (= i len)
77 acc
78 (let ((acc (f acc (bytevector-u8-ref vec i))))
79 (if (reduced? acc)
80 (unreduce acc)
81 (loop (+ i 1) acc)))))))
82
83 (define (port-reduce f identity reader port)
84 (let loop ((val (reader port)) (acc identity))
85 (if (eof-object? val)
86 acc
87 (let ((acc (f acc val)))
88 (if (reduced? acc)
89 (unreduce acc)
90 (loop (reader port) acc))))))
91
92 (define (generator-reduce f identity gen)
93 (let loop ((val (gen)) (acc identity))
94 (if (eof-object? val)
95 acc
96 (let ((acc (f acc val)))
97 (if (reduced? acc)
98 (unreduce acc)
99 (loop (gen) acc)))))))