Re: Quick question about SRFI-170 directory-files
Marc Feeley 29 Jul 2019 18:46 UTC
Marc
> On Jul 29, 2019, at 2:31 PM, Lassi Kortela <xxxxxx@lassi.io> wrote:
>
> That's cool. So you read it with 'read' like a Scheme port and it returns strings.
>
> A portable way to do the same nowadays might be SRFI 158 generators:
>
> (directory-generator "examples")
Gambit unifies such generators using ports. For example, another interesting one are FIFOs:
> (define fifo (open-vector))
> (write 111 fifo)
> (write 222 fifo)
> (read fifo)
111
> (read fifo)
222
> (write 333 fifo)
> (read fifo)
333
And another one is tcp-server ports whose elements are the connections received at the indicated interface and port number:
> (define server (open-tcp-server "localhost:12345"))
> (let loop ()
(let ((p (read server)))
(pretty-print p)
(display "hello\n" p)
(close-port p)
(loop)))
#<input-output-port #3 (tcp-client)> ;; after first “telnet localhost 12345”
#<input-output-port #4 (tcp-client)> ;; after second “telnet localhost 12345”
#<input-output-port #5 (tcp-client)> ;; after third “telnet localhost 12345”
Unifying generators through ports has the advantage of a single way to control waiting on the next value (rather than having a different API depending on the nature of the generator). In Gambit you can call (input-port-timeout-set! port timeout [thunk]) to set an action to perform if a read operation waits for longer than a certain amount of time. There’s also (output-port-timeout-set! port timeout [thunk]) for situations where writes may have to wait.
Marc