sub gron {
my ($f, $total, $width) = @_;
my $veet;
$veet = sub {
my ($partial, $subtotal, $n) = @_;
my $rem = $total - $subtotal;
if ($n+1 == $width) {
$f->($rem, @$partial);
}
else {
$veet->([$_, @$partial], $subtotal+$_, $n+1) for 0..$rem;
}
};
$veet->([], 0, 0);
}
gron(sub {print($_ ? $_ : " ") for @_; print "\n"}, 3, 27);
I'm fairly sure that the idea of using a recursive closure in Perl has never crossed my mind before. Notice the disguised conses as well
Recursive closures (Score:2)
B::Utilsfor a doubly-recursive closure. That broke my brane.Recursive closures (Score:2)
my $x; $x = sub { ... $x->(...) ... };
...you've just defined a circular data structure. So once you're finished with the whole excursion of calling $x, you have to undef $x or the sub-object doesn't get destroyed -- at least last I checked.
Re:Recursive closures (Score:1)
---ict / Spoon
Y Operator in Perl (Score:2)
Re: Y Operator in Perl (Score:1)