diff options
author | Case Duckworth | 2023-07-05 16:14:13 -0500 |
---|---|---|
committer | Case Duckworth | 2023-07-05 16:14:13 -0500 |
commit | 4a058e40b26c2b614ac68bad96a412809a7bac13 (patch) | |
tree | 61424936a7741c6acf59177afaf44a157a1b95f0 /chicanery.extras.scm | |
download | chicanery-4a058e40b26c2b614ac68bad96a412809a7bac13.tar.gz chicanery-4a058e40b26c2b614ac68bad96a412809a7bac13.zip |
Initial commit
Diffstat (limited to 'chicanery.extras.scm')
-rw-r--r-- | chicanery.extras.scm | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/chicanery.extras.scm b/chicanery.extras.scm new file mode 100644 index 0000000..24cdff9 --- /dev/null +++ b/chicanery.extras.scm | |||
@@ -0,0 +1,78 @@ | |||
1 | ;;; chicanery extras --- extra stuff from ur old pal acdw | ||
2 | |||
3 | (export list-map list-for-each list-append) | ||
4 | |||
5 | ;; Generalized map, for-each, ... | ||
6 | ;; List versions are renamed `list-<function>'. Un-prefixed versions work | ||
7 | ;; with any (default) datatype. TODO: generalize? | ||
8 | (define list-map map) | ||
9 | (define list-for-each for-each) | ||
10 | (define list-append append) | ||
11 | |||
12 | (define (map proc . collections) | ||
13 | (let ((first (car collections))) ; we only need to check the first one | ||
14 | (cond | ||
15 | ((list? first) | ||
16 | (apply list-map proc collections)) | ||
17 | ((vector? first) | ||
18 | (apply vector-map proc collections)) | ||
19 | ((string? first) | ||
20 | (apply string-map proc collections)) | ||
21 | (else (error "Bad datatype" first))))) | ||
22 | |||
23 | (define (for-each proc . collections) | ||
24 | (let ((first (car collections))) ; we only need to check the first one | ||
25 | (cond | ||
26 | ((list? first) | ||
27 | (apply list-for-each proc collections)) | ||
28 | ((vector? first) | ||
29 | (apply vector-for-each proc collections)) | ||
30 | ((string? first) | ||
31 | (apply string-for-each proc collections)) | ||
32 | (else (error "Bad datatype" first))))) | ||
33 | |||
34 | (define (append . collections) | ||
35 | (let ((first (car collections))) ; we only need to check the first one | ||
36 | (cond | ||
37 | ((list? first) | ||
38 | (apply list-append collections)) | ||
39 | ((vector? first) | ||
40 | (apply vector-append collections)) | ||
41 | ((string? first) | ||
42 | (apply string-append collections)) | ||
43 | ((bytevector? first) | ||
44 | (apply bytevector-append collections)) | ||
45 | (else (error "Bad datatype" first))))) | ||
46 | |||
47 | (export ref copy) | ||
48 | |||
49 | ;; Extended generic functions. | ||
50 | ;; These functions already have a list- version defined, but no non-prefixed | ||
51 | ;; one. So I'm fixing that. | ||
52 | (define (ref collection k) | ||
53 | (cond | ||
54 | ((list? collection) | ||
55 | (list-ref collection k)) | ||
56 | ((vector? collection) | ||
57 | (vector-ref collection k)) | ||
58 | ((string? collection) | ||
59 | (string-ref collection k)) | ||
60 | ((bytevector? collection) | ||
61 | (bytevector-u8-ref collection k)) | ||
62 | (else (error "Bad datatype" collection)))) | ||
63 | |||
64 | ;; I'm not going to generalize -copy! because I don't think it's a great idea, | ||
65 | ;; really. | ||
66 | (define (copy collection) | ||
67 | (cond | ||
68 | ((list? collection) | ||
69 | (list-copy collection)) | ||
70 | ((vector? collection) | ||
71 | (vector-copy collection)) | ||
72 | ((string? collection) | ||
73 | (string-copy collection)) | ||
74 | ((bytevector? collection) | ||
75 | (bytevector-copy collection)) | ||
76 | (else (error "Bad datatype" collection)))) | ||
77 | |||
78 | ;; TODO: look at set! semantics -- generalizable? | ||