diff options
author | Case Duckworth | 2023-08-29 21:54:21 -0500 |
---|---|---|
committer | Case Duckworth | 2023-08-29 21:54:21 -0500 |
commit | 9af3b2b445dfb7d03376bd01d52491175c20515e (patch) | |
tree | f56fcd725fb8d3d1e9a871f0b95e95e8848038a6 /chicanery.extras.scm | |
parent | Return to the good old working days (diff) | |
download | chicanery-9af3b2b445dfb7d03376bd01d52491175c20515e.tar.gz chicanery-9af3b2b445dfb7d03376bd01d52491175c20515e.zip |
Move chicanery.extras.scm -> extras.scm
Diffstat (limited to 'chicanery.extras.scm')
-rw-r--r-- | chicanery.extras.scm | 150 |
1 files changed, 0 insertions, 150 deletions
diff --git a/chicanery.extras.scm b/chicanery.extras.scm deleted file mode 100644 index ae2823e..0000000 --- a/chicanery.extras.scm +++ /dev/null | |||
@@ -1,150 +0,0 @@ | |||
1 | (export list-map list-for-each list-append | ||
2 | map for-each append) | ||
3 | |||
4 | (define list-map map) | ||
5 | (define list-for-each for-each) | ||
6 | (define list-append append) | ||
7 | |||
8 | (define (map proc . collections) | ||
9 | (let ((first (car collections))) ; we only need to check the first one | ||
10 | (cond | ||
11 | ((list? first) | ||
12 | (apply list-map proc collections)) | ||
13 | ((vector? first) | ||
14 | (apply vector-map proc collections)) | ||
15 | ((string? first) | ||
16 | (apply string-map proc collections)) | ||
17 | (else (error "Bad datatype" first))))) | ||
18 | |||
19 | (define (for-each proc . collections) | ||
20 | (let ((first (car collections))) ; we only need to check the first one | ||
21 | (cond | ||
22 | ((list? first) | ||
23 | (apply list-for-each proc collections)) | ||
24 | ((vector? first) | ||
25 | (apply vector-for-each proc collections)) | ||
26 | ((string? first) | ||
27 | (apply string-for-each proc collections)) | ||
28 | (else (error "Bad datatype" first))))) | ||
29 | |||
30 | (define (append . collections) | ||
31 | (let ((first (car collections))) ; we only need to check the first one | ||
32 | (cond | ||
33 | ((list? first) | ||
34 | (apply list-append collections)) | ||
35 | ((vector? first) | ||
36 | (apply vector-append collections)) | ||
37 | ((string? first) | ||
38 | (apply string-append collections)) | ||
39 | ((bytevector? first) | ||
40 | (apply bytevector-append collections)) | ||
41 | (else (error "Bad datatype" first))))) | ||
42 | |||
43 | ;;; Extended generic functions. | ||
44 | ;; These functions already have a list- version defined, but no non-prefixed | ||
45 | ;; one. So I'm fixing that. | ||
46 | (define (ref collection k) | ||
47 | (cond | ||
48 | ((list? collection) | ||
49 | (list-ref collection k)) | ||
50 | ((vector? collection) | ||
51 | (vector-ref collection k)) | ||
52 | ((string? collection) | ||
53 | (string-ref collection k)) | ||
54 | ((bytevector? collection) | ||
55 | (bytevector-u8-ref collection k)) | ||
56 | (else (error "Bad datatype" collection)))) | ||
57 | |||
58 | ;; I'm not going to generalize -copy! because I don't think it's a great idea, | ||
59 | ;; really. | ||
60 | (define (copy collection) | ||
61 | (cond | ||
62 | ((list? collection) | ||
63 | (list-copy collection)) | ||
64 | ((vector? collection) | ||
65 | (vector-copy collection)) | ||
66 | ((string? collection) | ||
67 | (string-copy collection)) | ||
68 | ((bytevector? collection) | ||
69 | (bytevector-copy collection)) | ||
70 | (else (error "Bad datatype" collection)))) | ||
71 | |||
72 | ;;; Definitions that should be in scheme | ||
73 | ;; This sections should be as small as possible | ||
74 | |||
75 | (export read-port | ||
76 | read-port-chunk-size) | ||
77 | |||
78 | |||
79 | (define (%read-port port) | ||
80 | (let ((chunk-size (read-port-chunk-size))) | ||
81 | (let loop ((next (read-string chunk-size port)) | ||
82 | (blank? #f) | ||
83 | (acc '())) | ||
84 | (cond | ||
85 | ((or (eof-object? next) | ||
86 | (and blank? (equal? next ""))) | ||
87 | (apply string-append (reverse acc))) | ||
88 | ((equal? next "") | ||
89 | (loop (read-string chunk-size port) | ||
90 | #t | ||
91 | (cons next acc))) | ||
92 | (else | ||
93 | (loop (read-string chunk-size port) | ||
94 | blank? | ||
95 | (cons next acc))))))) | ||
96 | |||
97 | (define read-port-chunk-size | ||
98 | (make-parameter 512)) | ||
99 | |||
100 | (define read-port | ||
101 | (case-lambda | ||
102 | (() (%read-port (current-input-port))) | ||
103 | ((p) (%read-port p)))) | ||
104 | |||
105 | (export atom? defined?) | ||
106 | |||
107 | (define (atom? x) | ||
108 | (not (or (null? x) | ||
109 | (pair? x)))) | ||
110 | |||
111 | (define (defined? sym) | ||
112 | (call-with-current-continuation | ||
113 | (lambda (k) | ||
114 | (with-exception-handler | ||
115 | (lambda (e) (k #f)) | ||
116 | (lambda () | ||
117 | (eval sym (interaction-environment)) | ||
118 | #t))))) | ||
119 | |||
120 | (export with-output-to-string | ||
121 | with-input-from-string) | ||
122 | |||
123 | (define (with-output-to-string thunk) | ||
124 | (call-with-port (open-output-string) | ||
125 | (lambda (port) | ||
126 | (parameterize ((current-output-port port)) | ||
127 | (thunk)) | ||
128 | (get-output-string port)))) | ||
129 | |||
130 | (define (with-input-from-string s thunk) | ||
131 | (call-with-port (open-input-string s) | ||
132 | (lambda (port) | ||
133 | (parameterize ((current-input-port port)) | ||
134 | (thunk))))) | ||
135 | |||
136 | (export displayed ->string written print) | ||
137 | |||
138 | (define (displayed x) | ||
139 | (with-output-to-string | ||
140 | (lambda () (display x)))) | ||
141 | |||
142 | (define (written x) | ||
143 | (with-output-to-string | ||
144 | (lambda () (write x)))) | ||
145 | |||
146 | (define (print . xs) | ||
147 | (for-each display xs) | ||
148 | (newline)) | ||
149 | |||
150 | (define ->string displayed) | ||