Should be noted that the initial problem of the OP (short-circuiting a call-back) is easily solved with goto
cbIterator { goto STOP if $_ >700; print; } \@nested; STOP:
Than there is some terminology confusion, in my books ( IIRC including Higher Order Perl ) this "pass a call-back" approach isn't an iterator but something I'd maybe call a diver or walker ¹, which does the iteration inside out by processing a call-back.
I.o.W for me in Perl speak:
iterator := iterator function
I checked Iterator on WP and of course there is still confusion, but at least someone proposed Iteratee
Iteratee, in which, instead of the developer calling the iterator repeatedly to get new values, the iteratee is called repeatedly to process new chunks of data - an example of inversion of control
The real problem with iterators in Perl is how to continue them after the first stop. (The OP didn't ask this)
Thats why Perl6 introduces gather/take and Python has "generators" with yield constructs.
Since you're flattening the recursion into a loop with its own call @stack , this can easily be used for such a "coroutine".
The trick is to store persisting data like @stack in the surrounding closure of the generator!
Hope this was of interest. :)
¹) I'd love to be pointed to some sources clarifying the terminology.
In reply to Re^2: An iterator for (not "iterating") a recursive data structure.
by LanX
in thread An iterator for (not "iterating") a recursive data structure.
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |