<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8">
</head>
<body>
<div style="font-family:sans-serif"><div style="white-space:normal">
<p dir="auto">Wouldn't this technique have trouble with code movement issues? For example, if I have<br>
<code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7"><br>
(iter (for x in data-table)<br>
(averaging x into mean)<br>
(finally (return mean)))<br>
</code><br>
I know that's a contrived example, but I think the point is clear: if I'm going to make <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">averaging</code> a collector, I'd like to be able to use its result, and if that result is computed in a <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">finally</code> block that's invisible and out of my control, I can't do it, can I? There's <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">finally-protected</code>, but I think that's the <em>opposite</em> of what I need.</p>
<p dir="auto">Best,<br>
r</p>
<p dir="auto">On 13 Mar 2018, at 10:08, Russ Tyndall wrote:</p>
</div>
<div style="white-space:normal"></div>
<blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><div id="8D57645D-6DBE-4C70-B0AF-67BBBF94AC16">
<div text="#000000" bgcolor="#FFFFFF">
<p>Here is an existing "sampling" clause to pull a random sample
from a larger data set. The long and short is just use a finally
clause, as you would when writing a normal iterate loop.</p>
<p>(iterate:defmacro-clause (sampling expr &optional into var
size size)<br>
"resevoir sample the input"<br>
(let ((sample (or var iterate::*result-var*)))<br>
(alexandria:with-unique-names (i sample-size sigil buffer row)<br>
`(progn<br>
(with ,sample)<br>
(with ,sample-size = (or ,size 100))<br>
(with ,buffer = (make-array ,sample-size :initial-element
',sigil))<br>
(with ,i = 0)<br>
(if (< ,i ,sample-size)<br>
(setf (aref ,buffer ,i) ,expr)<br>
(let ((r (random ,i)))<br>
(when (< r ,sample-size)<br>
(setf (aref ,buffer r) ,expr))))<br>
(incf ,i)<br>
(finally<br>
;; convert our sample to a list, but only if we actually
took the sample<br>
(when (plusp ,i)<br>
(setf ,sample<br>
(iter (for ,row in-vector ,buffer)<br>
(until (eq ,row ',sigil))<br>
(collect ,row)))))))))<br>
<br>
</p>
Cheers,<br>
Russ Tyndall<br>
Acceleration.net<br>
<div class="moz-cite-prefix">On 03/13/2018 10:49 AM, Robert Goldman
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:06690B11-F59C-417B-A7BD-CAE3836CBD3E@sift.info">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<div style="font-family:sans-serif">
<div style="white-space:normal">
<p dir="auto">I was going to define an <code
style="background-color:#F7F7F7; border-radius:3px;
margin:0; padding:0 0.4em" bgcolor="#F7F7F7">AVERAGING</code>
collector clause for iterate, but I'm not sure how to do it.
The obvious thing, it seemed to me, would be to sum the
values as I go along, and count them, and then divide the
sum by the count when leaving the loop.</p>
<p dir="auto">But the examples for <code
style="background-color:#F7F7F7; border-radius:3px;
margin:0; padding:0 0.4em" bgcolor="#F7F7F7">DEFMACRO-CLAUSE</code>
in the manual do all of their work while iterating, and
there doesn't seem to be an "at-end" hook. Is the kind of
thing I would like feasible, and if so, how is it to be
done?</p>
<p dir="auto">thanks!<br>
r</p>
</div>
</div>
</blockquote>
<br>
</div></div></blockquote>
<div style="white-space:normal">
<blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px">
</blockquote></div>
<div style="white-space:normal">
</div>
</div>
</body>
</html>