I don't like the prototype in this case either, it heralds too many limitations in use. The practical ones of artificially limiting the number of arrays it can operate on, but more fundementally, the fact that you can only use real arrays.
With the caveat of requiring the user to pass an AoA ref, the function becomes much more flexible and makes writing transpose LOL trivial:
#! perl -slw
use strict;
use Data::Dumper::SLC;
sub multimap (&$) {
my( $code, $aref ) = @_;
return unless ref $aref eq 'ARRAY';
map {
my $i = $_;
$code->( map{ $_->[ $i ] } @$aref )
} 0 .. $#{ $aref->[ 0 ] };
}
my @l = 'a' .. 'h';
my @u = 'A' .. 'H';
my @n = 1 .. 8;
print for multimap { join ', ', @_ } [ \( @u, @n, @l ) ];
sub transpose {
return multimap{ [ @_ ] } $_[ 0 ];
}
my @LoL = ([1,2,3],[2,3,5]);
my @transposed = transpose \@LoL;
Dump \@transposed;
__END__
P:\test>464573
A, 1, a
B, 2, b
C, 3, c
D, 4, d
E, 5, e
F, 6, f
G, 7, g
H, 8, h
[ [ '1', '2', ], [ '2', '3', ], [ '3', '5', ], ]
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
|