in reply to Re^3: Cartesian Cross-Products
in thread Cartesian Cross-Products
Can be simplified to:my @C = map { [ $_ ] } @{ shift @_ };
Don't believe me? Try it. Also, it now works correctly in the case you pass no arguments to the function. This may be a matter of taste, but I prefer the leftmost array to be the outermost loop. So that leaves us with:my @C = [];
sub cartesian { my @C = []; foreach (reverse @_) { my @A = @$_; @C = map { my $n = $_; map { [ $n, @$_ ] } @C } @A; } return @C; }
|
---|