in reply to Re: Re (tilly) 5: Why are closures cool?
in thread Why are closures cool?
As for the second, it is possible already:
OK, so the cost is that you have to use anonymous functions and a slightly unusual syntax. But that is fixable as well - just use the syntax all of the time and it is no longer unusual for you! (This has been suggested seriously before because it allows strict.pm to catch typos in function names.)sub recurse_postorder { my $self=shift; my $debug=shift; my @list; my $recurser; $recurser = sub { my $node=shift; my $depth=shift; print ("\t"x$depth)."$node->{value}\n" if $debug; $recurser->($node->{left},$depth+1); $recurser->($node->{right},$depth+1); push @list,$node->{value}; }; $recurser->($self->{root},0); return \@list; }
The bigger cost is that the function is never garbage collected. You can partially fix this by doing an undef on the function. I say partially because there is an outstanding bug where it still doesn't get garbage collection. But ignoring the garbage issue, it is here and works. :-)
|
|---|