in reply to Re: cross combinations
in thread cross combinations
You can code the exact same thing in the same number of lines in p6. That was not the real point though. I don't think anyone was golfing. The p6 solution above has something yours doesn't, take and gather. By default (at least last week ;) ) take and gather are lazy.
sub cross (*$f, *@a) { return unless $f; @a or return @{$f}.map:{[$_]} ; # Handle one argument. my @r = cross(@a); # Recurse. @$f.map:{my $l = $_ ; @r.map:{[$l, @$_]} }; # Distribute. } cross().perl.say; cross([],[1]).perl.say; cross([1,2]).perl.say; cross([1,2],[3]).perl.say; cross([1],[2],[3]).perl.say; cross([1],[2],[3,4]).perl.say;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: cross combinations
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 |