in reply to Re: An iterator for (not "iterating") a recursive data structure.
in thread An iterator for (not "iterating") a recursive data structure.
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: An iterator for (not "iterating") a recursive data structure. (Winner!)
by BrowserUk (Patriarch) on Feb 12, 2015 at 15:01 UTC | |
|
Re^3: An iterator for (not "iterating") a recursive data structure. (terser)
by ikegami (Patriarch) on Feb 13, 2015 at 15:30 UTC | |
|
Re^3: An iterator for (not "iterating") a recursive data structure. (index into keys?)
by LanX (Saint) on Jun 18, 2020 at 20:48 UTC | |
by LanX (Saint) on Jun 19, 2020 at 00:32 UTC |