Note that you can drop the ", 0", the "ref" and both of the "next;"s.
Since you just need pairs, you can even drop the "["s and "]"s, reducing the code to:
sub genIterator { my( $root ) = @_; my @stack = ( $root, 0 ); return sub { while( @stack ) { my $index = pop @stack; my $tree = pop @stack; if( ref( $tree->[$index] ) ) { push( @stack, $tree, $index+1, $tree->[$index], 0 ); } elsif( $index < @$tree ) { push( @stack, $tree, $index+1 ); return $tree->[$index]; } } return; # empty stack } }
(Not some fundamental change, just a minor reworking.)
- tye
In reply to Re^2: An iterator for (not "iterating") a recursive data structure. (terser)
by tye
in thread An iterator for (not "iterating") a recursive data structure.
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |