From: Robert Monfera
Subject: How to unwind-protect series functions
Date: 
Message-ID: <3834E095.14D5DCBA@fisec.com>
Hello series users,

I would like to know how it is the best to achieve the effects of
unwind-protect with series.  Basically, I can not know if or when the
application will reach the end of a series, due to its lazy nature.

For example I do this (OK, I don't really):

(defun scan-numbers (list-of-numbers)
  (declare (optimizable-series-function 1))
  (scan list-of-numbers))

(defun output-numbers (list-of-numbers max-length)
  (iterate 
    ((number (subseries (scan-numbers list-of-numbers) 0 max-length))
    (print number)))

I want to ensure that an action is taken after scanning in the last
number, and that this action is defined inside scan-numbers, so as to
allow various functions for further processing or outputting without
having to deal with cleanup.

Basically, this function is implemented for scan-file (quote from
Waters' Series section in CLtL2):

"scan-file file-name &optional (reader #'read)

scan-file opens the file named by the string file-name and applies the
function reader to it repeatedly until the end of the file is reached.
Reader must accept the standard input function arguments input-stream,
eof-error-p, and eof-value as its arguments. (For instance, reader can
be read, read-preserving-white-space, read-line, or read-char.) If
omitted, reader defaults to read. scan-file returns a series of the
values returned by reader, up to but not including the value returned
when the end of the file is reached. The file is correctly closed, even
if an abort occurs."

I looked at the definitions of scan-file and scan-stream, but it did not
really help.  Probably the file stream would be closed properly if there
was a truncation.  Unwind-protect is not being referred to in the series
implementation (OK, with-open-file is used, but that is specific to
scan-file, and it seems scan-stream does not do any clean-up).

I was thinking about collect-last or something similar, but I don't see
how to do it given that I want to specify cleanup inside scan-numbers. 
I am wondering if the best option would be to somehow make the data flow
analyzer aware of unwind-protect, to end up with a generic and
transparent solution?  I.e.,. before doing a (go END), the second
argument of unwind-protect would get evaluated, and maybe multiple
unwind-protects would somehow combine, as they become part of one big
LOOP.

Of course, in the current implementation this prevents series from being
optimized:

(defun scan-numbers (list-of-numbers)
  (declare (optimizable-series-function 1))
  (unwind-protect 
    (scan list-of-numbers)
    (user:clean-up)))

The reason is that the data flow will cease to be straight.

Any help is appreciated.

Thanks,
Robert

From: Lieven Marchand
Subject: Re: How to unwind-protect series functions
Date: 
Message-ID: <m3wvrd8jh7.fsf@localhost.localdomain>
Robert Monfera <·······@fisec.com> writes:

> Hello series users,
> 
> I would like to know how it is the best to achieve the effects of
> unwind-protect with series.  Basically, I can not know if or when the
> application will reach the end of a series, due to its lazy nature.
> 

Have you looked at section A.4 in CLtL2? More precisely, the macro
ENCAPSULATED. I'm not quite sure I understand what you want to do, but
the example given implements a simple-collect-file using
WITH-OPEN-FILE but there's no reason you couldn't use UNWIND-PROTECT
instead.

-- 
Lieven Marchand <···@bewoner.dma.be>
If there are aliens, they play Go. -- Lasker
From: Robert Monfera
Subject: Re: How to unwind-protect series functions
Date: 
Message-ID: <383708C3.84075BBB@fisec.com>
Lieven Marchand wrote:
...
> Have you looked at section A.4 in CLtL2? More precisely, the macro
> ENCAPSULATED.

Yes, that's most probably what I need.  I should have RTFM more
exhaustively before asking.  (I haven't yet got time to test it though.) 
It is great that a lot of thought went into this package, and there are
people who are so familiar with it, despite it's not in the standard.

Thanks,
Robert