chicken-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-hackers] Suggestion for the ports unit: port-pipe


From: Moritz Heidkamp
Subject: [Chicken-hackers] Suggestion for the ports unit: port-pipe
Date: Mon, 09 Aug 2010 04:02:19 +0200

Dear Chickenados,

I frequently find myself wanting/implementing a procedure which I shall
call `port-pipe'. As the name suggests, its purpose is piping data from
one port to another. As I find it very handy and general-purposey, I
propose it for inclusion in the ports unit. Without further ado, here is
the procedure:

    (define (port-pipe #!key (from (current-input-port)) (to 
(current-output-port)) (read read-char) (write write-char))
      (with-output-to-port to
        (lambda ()
          (with-input-from-port from
            (lambda ()
              (port-for-each write read))))))

Usage examples: 

    (call-with-input-request "http://call-cc.org/"; #f (cut port-pipe))

    (call-with-input-file "foo" 
      (lambda (file) (port-pipe from: file to: (current-error-port))))

Okay, some further ado: 

AFAIK, read-char/write-char are not the most performant options compared
to say read-line/write-line, but it's the most robust default since it
pipes contents unaltered as opposed to write-line which always appends a
newline even if there was none in the input previously read by read-line
(this unorthogonality is maybe not quite optimal but changing
read-line's behavior would probably break a lot of code so I refrain
from suggesting it).

I am neither married to the signature nor the default arguments nor the
implementation, so comments are welcome. Also let me know if this
procedure might be completely silly and useless. I'd love to know why
though :-)

I have a few tests lying around for this, too.

Moritz



reply via email to

[Prev in Thread] Current Thread [Next in Thread]