about summary refs log tree commit diff stats
path: root/extras.scm
blob: 423c78f7db0a5bad6394201363faf1ee464ee861 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
;;; Chicanery extras

(export slurp)

(define slurp
  (case-lambda
    (() (slurp (current-input-port)))
    ((port)
     (let loop ((ch (read-char port))
                (acc '()))
       (if (eof-object? ch)
           (list->string (reverse acc))
           (loop (read-char port)
                 (cons ch acc)))))))


(export with-output-to-string
        with-input-from-string)

(define (with-output-to-string thunk)
  (call-with-port (open-output-string)
    (lambda (port)
      (parameterize ((current-output-port port))
        (thunk))
      (get-output-string port))))

(define (with-input-from-string s thunk)
  (call-with-port (open-input-string s)
    (lambda (port)
      (parameterize ((current-input-port port))
        (thunk)))))


(export displayed ->string
        written
        print)

(define (displayed x)
  (with-output-to-string
    (lambda () (display x))))

(define ->string displayed)

(define (written x)
  (with-output-to-string
    (lambda () (write x))))

(define (print . xs)
  (for-each display xs)
  (newline))