From 14847f7800a477544df8ea16bf5fec1d9ceddea0 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Wed, 9 Aug 2023 22:00:31 -0500 Subject: Add with-{input-from,output-to}-string Inspired by CHICKEN --- chicanery.extras.scm | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'chicanery.extras.scm') 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 @@ (eval sym (interaction-environment)) #t))))) -(define (displayed x) +(define (with-output-to-string thunk) (call-with-port (open-output-string) (lambda (port) - (display x port) + (parameterize ((current-output-port port)) + (thunk)) (get-output-string port)))) -(define (written x) - (call-with-port (open-output-string) +(define (with-input-from-string s thunk) + (call-with-port (open-input-string s) (lambda (port) - (write x port) - (get-output-string port)))) + (parameterize ((current-input-port port)) + (thunk))))) + +(define (displayed x) + (with-output-to-string + (lambda () (display x)))) + +(define (written x) + (with-output-to-string + (lambda () (write x)))) (define (print . xs) (for-each display xs) -- cgit 1.4.1-21-gabe81