<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body 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>
</body>
</html>