Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: Question about recursively generated iteratorsby ikegami (Patriarch) |
on Sep 21, 2007 at 15:52 UTC ( [id://640390]=note: print w/replies, xml ) | Need Help?? |
I tried to see if I could find a generic way of transforming a recursive function into an iterator. I found a method that's can be applied rather mechanically. Let's say your recursive function visits a tree in preorder. The following would be the a non-iterative solution:
Change the visitor code to return \$val; where $val is the data to return from the iterator. return \@val; is also acceptable if you wish to return more than one value. Break up the function where a recursive call is made. Return each block as a function as follows:
In this case, it can be simplified a little.
Now we just need an engine to drive this.
Finally, an example caller.
In Section
Seekers of Perl Wisdom
|
|