foreach, an analogy
Andy Knowles
andy.knowles at gmail.com
Thu Oct 19 17:04:15 PDT 2006
Karen Lanrap wrote:
> Sean Kelly wrote:
>
>> The iterator would have to contain a stack of pointers to
>> previously visited nodes.
>
> But that holds for "foreach" also.
No, it doesn't:
class Tree {
Tree left;
Tree right;
int data;
int opApply(int delegate(inout Tree n) dg) {
int ret = dg(this);
if(!ret && left != null)
ret = left.opApply(dg);
if(!ret && right != null)
ret = right.opApply(dg);
return ret;
}
}
The call stack stores our stack implicitly. This is the difference
between for loops and foreach - it doesn't much change the way one
writes a loop but it can drastically change the way one writes an iterator.
More information about the Digitalmars-d-announce
mailing list