in reply to cross combinations
However, I think that the cross product of nothing should be the empty set ([]), which would make the function even simpler as it loses a special case:sub cross { my $f = shift or return; # Nothing. @_ or return map {[$_]} @{$f}; # Handle one argument. my @r = cross(@_); # Recurse. map {my $l = $_; map {[$l, @$_]} @r} @$f; # Distribute. }
sub cross { my $f = shift or return []; # Nothing. my @r = cross(@_); # Recurse. map {my $l = $_; map {[$l, @$_]} @r} @$f; # Distribute. }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: cross combinations
by eric256 (Parson) on Oct 28, 2005 at 14:23 UTC | |
by Perl Mouse (Chaplain) on Oct 28, 2005 at 15:00 UTC | |
by eric256 (Parson) on Oct 28, 2005 at 18:10 UTC | |
by tilly (Archbishop) on Oct 29, 2005 at 03:04 UTC | |
|
Re^2: cross combinations
by xdg (Monsignor) on Oct 28, 2005 at 11:42 UTC | |
by Perl Mouse (Chaplain) on Oct 28, 2005 at 11:56 UTC |