in reply to Re: An iterator for (not "iterating") a recursive data structure.
in thread An iterator for (not "iterating") a recursive data structure.
This [...] only works if undef is not a possible value, and empty arrays are not possible.
It doesn't even handle false values (without a minor change).
The following handles false values (including undef) as well as empty arrays:
sub genIterator { my $array = shift; my $index = 0; # Current index into $array my $child_iter; # Iterator for array at $array[$index] return sub { while (1) { # If we're iterating through descendants of a child, if (defined $child_iter) { # If the there is a descendant we haven't returned, if ( my ($element) = $child->() ) { return $element; } # No more descendants for this child. $child_iter = undef; } # Signal end of iteration if done. return if $index >= @$array; # Return next child if it's not an array ref. my $element = $array->[$index++]; return $element if ref $element ne 'ARRAY'; # Prepare to iterate through descendants of child. $child_iter = genIterator($element); } } } my $iter = genIterator \@nested; say while ($_) = $iter->();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: An iterator for (not "iterating") a recursive data structure.
by Eily (Monsignor) on Jul 09, 2019 at 07:08 UTC | |
by LanX (Saint) on Jul 09, 2019 at 08:46 UTC | |
by Eily (Monsignor) on Jul 09, 2019 at 09:29 UTC | |
by LanX (Saint) on Jul 09, 2019 at 10:37 UTC | |
by ikegami (Patriarch) on Jul 09, 2019 at 18:38 UTC | |
|