Thanks for the fix and suggestions.<br><br><div class="gmail_quote">On Fri, Jan 21, 2011 at 1:37 PM, Matthew Flatt <span dir="ltr"><<a href="mailto:mflatt@cs.utah.edu">mflatt@cs.utah.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
At Fri, 21 Jan 2011 10:53:42 -0700, Doug Williams wrote:<br>
> What is the best way to perform actions after a sequence terminates? Here is<br>
> a simple example that is like in-lines, but takes a path instead of a port.<br>
> It works, but I was wondering if there is a better way to do it.<br>
><br>
> (define (in-file-lines path)<br>
> (let ((port (open-input-file path #:mode 'text)))<br>
> (make-do-sequence<br>
> (lambda ()<br>
> (values<br>
> (lambda (_) (read-line port 'any))<br>
> void<br>
> (void)<br>
> void<br>
> (lambda _ (if (eof-object? (peek-byte port))<br>
> (begin<br>
> (close-input-port port)<br>
> #f)<br>
> #t))<br>
> void)))))<br>
<br>
I think you mean<br>
<br>
(define (in-file-lines path)<br>
(let ((port (open-input-file path #:mode 'text)))<br>
(make-do-sequence<br>
(lambda ()<br>
(values<br>
(lambda (_) (read-line port 'any))<br>
void<br>
(void)<br>
void<br>
(lambda (v) (if (eof-object? v) ; <----------<br>
(begin<br>
(close-input-port port)<br>
#f)<br>
#t))<br>
void)))))<br>
<br>
so that the last line is included in the sequence. Otherwise, I don't<br>
have any better suggestions.<br>
<br>
Beware that if the sequence stops being used before you get to the end<br>
of the file, then the port won't be closed:<br>
<br>
(for ([line (in-file-lines <file>)]<br>
[elem (in-list '(1 2 3))])<br>
...)<br>
; port isn't closed if it has more than 3 lines<br>
<br>
But the sequence protocol doesn't give a sequence any way to know that<br>
it isn't being used, other than GC-based finalization --- which is<br>
probably a bad idea for file ports, since you can run out of available<br>
file descriptors much faster than available memory.<br>
<br>
</blockquote></div><br>