guile-user
[Top][All Lists]
Advanced

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

Re: Question about an error with ports


From: Zelphir Kaltstahl
Subject: Re: Question about an error with ports
Date: Thu, 10 Mar 2022 22:32:04 +0000

Hello Olivier!

On 3/10/22 16:05, Olivier Dion wrote:
On Thu, 10 Mar 2022, Zelphir Kaltstahl <zelphirkaltstahl@posteo.de> wrote:

I have the following questions:

(1) Is the read-from-write-to procedure useful at all, or is there a better way
to get all stuff from an input port and output it to an output port, avoiding
possibly large string values?
On Linux, there's slice(2) for pipes that does exactly that.

Otherwise, I think this version is a better fit

(define splice
   (lambda (in-port out-port)
     "Read from an IN-PORT and write to OUT-PORT"
     (let loop ([bv (get-bytevector-some in-port)])
       (unless (eof-object? bv)
         (put-bytevector out-port bv)
         (loop (get-bytevector-some in-port))))))

you should not close OUT-PORT.  This break the REPL in my case if using
the `current-output-port`.

Thanks! That solves the problem. I guess a caller of the function can decide themselves, when they want to close the port, so no need to close it inside that procedure. Closing it inside the procedure would also only limit its use, because it could only be used with ports, which are OK to close.

When not closing the port, using (unless ...) also makes more sense than what I 
had.

Just one question: Why is get-bytevector-some better than get-bytevector-n and specifying a number of bytes?

Best regards,
Zelphir

--
repositories: https://notabug.org/ZelphirKaltstahl




reply via email to

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