<div dir="ltr"><div>Just a curiosity: you could compute the average incrementally: <<a href="https://math.stackexchange.com/questions/106700/incremental-averageing">https://math.stackexchange.com/questions/106700/incremental-averageing</a>>, but doing it the usual way is probably more efficient and with smaller numeric error when using floats.</div><div><br></div><div>Slightly off-topic question: oftentimes I have to use map-like iterators. Is there a good way to use such iterators in conjunction with iter? An obvious way would be something like:</div><div><br></div><div> (iter (repeat 1) (mapc (lambda (x) (collect x)) '(1 2 3)))</div><div><br></div><div>The (iter (repeat 1) ...) bit could be hidden under a macro, I suppose. But it'd be nicer if collect were a function, so I could do (mapc #'collect '(1 2 3)). Perhaps said macro could rewrite #'clause to (lambda (x) (expansion-of-clause x)) or something similar. Looks like a fun idea to explore so I'm wondering if someone's played with something like this before.</div><div><br></div><div>(It's even more fun to turn map-like iterators into generators using continuations, but that's fully off-topic. :-))</div><div><br></div><div>Cheers,</div><div>Luís</div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Mar 13, 2018 at 3:08 PM Russ Tyndall <<a href="mailto:russ@acceleration.net">russ@acceleration.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<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</div><div text="#000000" bgcolor="#FFFFFF"><br>
<div class="m_-2021505211364927429moz-cite-prefix">On 03/13/2018 10:49 AM, Robert Goldman
wrote:<br>
</div>
<blockquote type="cite">
<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></blockquote></div>