On Tue, 14 Sep 2004 12:45:17 +0200 (CEST), Michele Dondi
<[EMAIL PROTECTED]> wrote:
> Now I want to take a list of templates, say $t1, ... $tn and get the
> result of
>
> $result = pack $tn, ... pack $t2, pack $t1, @input;
>
> without actually writing the whole thing. To my knowledge and great
> pleasure Perl6 will support currying and a builtin reduce/fold operator.
You could do this with a recursive function (They're your friends. Really.).
sub extended_pack( [EMAIL PROTECTED] = ($t1, ... , $tn), [EMAIL PROTECTED] ) {
# start with the last template so we can use tail recursion
@input = pack pop(@templates), @input;
# if there aren't any more templates, we're done
return @input if not @templates;
return extended_pack(@templates, [EMAIL PROTECTED]);
}
Or in Perl 5, which has to use 2 subs to have the same interface (code
untested):
sub extended_pack {
my (@input) = @_;
my @templates = ($t1, ..., $tn);
return extended_pack_rec([EMAIL PROTECTED], [EMAIL PROTECTED]);
}
sub extended_pack_rec {
my ($input, $templates) = @_;
@$input = pack pop(@$templates), @$input;
return @$input if not @$templates;
# special goto magic for tail recursion
@_ = ($input, $templates);
goto &extended_pack_rec;
}
I prefer Perl 6.
--
matt