(quoted from Darren) One thing I would like to be able to do is this: @baz = cross([1,2],[3,4]); # yields ([1,3],[1,4],[2,3],[2,4]) And alternately, this: for cross([1,2],[3,4]) -> $foo,$bar { ... } # loop has 4 iterations More examples: cross() # yields () cross([],[1]) # yields () cross([1,2]) # yields ([1],[2]) cross([1,2],[3]); # yields ([1,3],[2,3]) cross([1],[2],[3]) # yields ([1,2,3]) cross([1],[2],[3,4]) # yields ([1,2,3],[1,2,4])
Warning: Perl6 code ahead.
Update: the default 1 goes first, I guesssub cross { my @list = @_.reverse or return; gather { my $count = [*] 1, @_.map:{ .elems }; for 0 .. $count - 1 -> $idx is copy { take [ @_.map:{ ($idx, my $elem) = divmod($idx, .elems); .[$e +lem] } ].reverse; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: cross combinations
by Perl Mouse (Chaplain) on Oct 28, 2005 at 09:52 UTC | |
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 | |
by xdg (Monsignor) on Oct 28, 2005 at 11:42 UTC | |
by Perl Mouse (Chaplain) on Oct 28, 2005 at 11:56 UTC | |
|
Re: cross combinations (lazy)
by tye (Sage) on Oct 28, 2005 at 13:43 UTC | |
by revdiablo (Prior) on Oct 28, 2005 at 14:26 UTC | |
by tye (Sage) on Oct 28, 2005 at 15:08 UTC | |
by revdiablo (Prior) on Oct 28, 2005 at 16:33 UTC | |
by tye (Sage) on Oct 28, 2005 at 17:49 UTC | |
|
Re: cross combinations
by chas (Priest) on Oct 29, 2005 at 06:31 UTC | |
|
Re: cross combinations
by Courage (Parson) on Nov 02, 2005 at 18:33 UTC |