[stringtemplate-interest] Bug in rest function when applied recursively
Zenaan Harkness
zen at freedbms.net
Thu Sep 28 18:57:22 PDT 2006
On Thu, Sep 28, 2006 at 03:02:50PM -0400, John Snyders wrote:
> I was thinking about how templates can be called recursively. Just for fun I
> decided to try the classic recursive definition of reverse.
> group Reverse;
> reverse(L) ::= <<
> $if(L)$
> $reverse(rest(L))$$first(L)$
> $endif$
> >>
...
> I changed rest to return a new collection (ArrayList) that contains the rest
> of the original collection. After making this change the reverse template
> worked perfectly.
>
> The trouble is that creating a new collection is wasteful. Perhaps there is
> a way to determine if a copy of rest is needed rather than just an iterator.
> For example just at the point where the iterator is going to be passed to a
> template it is copied to a new collection. Are there any other cases where
> an iterator should be turned to a collection?
>
> If Java iterators supported cloning (clone would create a new iterator over
> the same colection positioned at the point where the original iterator was
> cloned) it may be enough to always return a cloned iterator from rest. But
> Java iterators don?t allow cloning.
Create a CloneableArrayListIterator, etc (or whatever iterators
are needed).
It's only the built-in iterators which don't support cloning, of
course :)
Cloning the iterator sounds like it might not introduce too much
of a performance hit.
Creating a new collection does sound awefully heavy...
cheers
zen
--
Free Australia - www.UPMART.org
Please respect the confidentiality of this email as sensibly warranted.
More information about the stringtemplate-interest
mailing list