diff options
author | Case Duckworth | 2023-08-09 22:00:31 -0500 |
---|---|---|
committer | Case Duckworth | 2023-08-09 22:00:31 -0500 |
commit | 14847f7800a477544df8ea16bf5fec1d9ceddea0 (patch) | |
tree | b974ec83fa0670014a0277451fa05ea0ba49f977 | |
parent | Update README (diff) | |
download | chicanery-14847f7800a477544df8ea16bf5fec1d9ceddea0.tar.gz chicanery-14847f7800a477544df8ea16bf5fec1d9ceddea0.zip |
Add with-{input-from,output-to}-string
Inspired by CHICKEN
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | chicanery.extras.scm | 21 | ||||
-rw-r--r-- | chicanery.sld | 2 |
3 files changed, 19 insertions, 6 deletions
diff --git a/README.md b/README.md index c8b1fcd..c275ffb 100644 --- a/README.md +++ b/README.md | |||
@@ -34,6 +34,8 @@ Other extras include | |||
34 | - `(read-port)`, `(read-port port)` reads a port until hitting end-of-file (IDK | 34 | - `(read-port)`, `(read-port port)` reads a port until hitting end-of-file (IDK |
35 | why this isn't in R7RS!), in chunks of `read-port-chunk-size` | 35 | why this isn't in R7RS!), in chunks of `read-port-chunk-size` |
36 | - `(defined? x)` returns whether the symbol `x` is bound to a variable | 36 | - `(defined? x)` returns whether the symbol `x` is bound to a variable |
37 | - `(with-input-from-string str thunk)` calls `thunk` with `str` bound as the current-input-port. | ||
38 | - `(with-output-to-string thunk)` calls `thunk` and returns a string of the output. | ||
37 | - `(displayed x)`, `(->string x)` returns `x` as a string (via `display`) | 39 | - `(displayed x)`, `(->string x)` returns `x` as a string (via `display`) |
38 | - `(written x)` returns `x` as a string (via `write`) | 40 | - `(written x)` returns `x` as a string (via `write`) |
39 | - `(print x ...)` displays `x ...` followed by a newline | 41 | - `(print x ...)` displays `x ...` followed by a newline |
diff --git a/chicanery.extras.scm b/chicanery.extras.scm index dac823f..f264424 100644 --- a/chicanery.extras.scm +++ b/chicanery.extras.scm | |||
@@ -113,17 +113,26 @@ | |||
113 | (eval sym (interaction-environment)) | 113 | (eval sym (interaction-environment)) |
114 | #t))))) | 114 | #t))))) |
115 | 115 | ||
116 | (define (displayed x) | 116 | (define (with-output-to-string thunk) |
117 | (call-with-port (open-output-string) | 117 | (call-with-port (open-output-string) |
118 | (lambda (port) | 118 | (lambda (port) |
119 | (display x port) | 119 | (parameterize ((current-output-port port)) |
120 | (thunk)) | ||
120 | (get-output-string port)))) | 121 | (get-output-string port)))) |
121 | 122 | ||
122 | (define (written x) | 123 | (define (with-input-from-string s thunk) |
123 | (call-with-port (open-output-string) | 124 | (call-with-port (open-input-string s) |
124 | (lambda (port) | 125 | (lambda (port) |
125 | (write x port) | 126 | (parameterize ((current-input-port port)) |
126 | (get-output-string 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)))) | ||
127 | 136 | ||
128 | (define (print . xs) | 137 | (define (print . xs) |
129 | (for-each display xs) | 138 | (for-each display xs) |
diff --git a/chicanery.sld b/chicanery.sld index 7835e80..a38b1e5 100644 --- a/chicanery.sld +++ b/chicanery.sld | |||
@@ -382,6 +382,8 @@ | |||
382 | read-port | 382 | read-port |
383 | read-port-chunk-size | 383 | read-port-chunk-size |
384 | defined? | 384 | defined? |
385 | with-output-to-string | ||
386 | with-input-from-string | ||
385 | displayed ->string | 387 | displayed ->string |
386 | written | 388 | written |
387 | print) | 389 | print) |