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