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; }
In reply to Re^4: Cartesian Cross-Products
by Anonymous Monk
in thread Cartesian Cross-Products
by japhy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |