[iterate-devel]/[series-users] collect-tail ?

Hoehle, Joerg-Cyril Joerg-Cyril.Hoehle at t-systems.com
Mon Dec 19 09:39:58 UTC 2005


Chris,

since you're asking similar questions in series-users and iterate-devel, I'm starting to wonder whether you're going to write a paper on comparisons of some looping frameworks? :-)

>how do I go about collecting the last N elements of anything?
>  (iter (for line :in-file my-file :using #'read-line)
>        (collect-tail line :how-many 10))

If 10 is known at macro-expansion time, you can always write a macro that expands to a series of 10 uses of PREVIOUS in Iterate, or 10 consecutive latches in Series.

I.e., IMHO there are always two ways, either with series of iterate:

a) Either take a code generation approach and create code that latches (= delay by one) N times,

b) Or take a functional approach, implement a ring datastructure and use that.

The former looks like a better use of Series: it could be a series of the 10 last values (as a list, or 10 series of values farther and farther behind).

In/After the last iteration, you'll have the 10 last values.

As to which approach is better... Probably you wouldn't want to generate code for the last 100 values.

BTW, you'll have to defined what happens when less than 10 iterations were taken. Straight-forward code is likely to return (val1 val2 nil nil ...6xnil), whereas one could wish for (val1 val2) with only 2 iterations.

Regards,
	Jorg Hohle.



More information about the iterate-devel mailing list