in reply to Perl6 Contest #2: P6 That Doesn't Look Like P5
What you're searching for is the outer product. Following code should do it, and works in current Pugs :)
use v6; sub outer(*@vals) { my &helper = -> @prev, @rest { if @rest { # We've still got a rest to interate over. # We add all items of @rest[0] to the new @prev # in our sub-helper, and use @rest[1...] as new # rest, i.e. all elements of @rest except the # first one. helper [ *@prev, $_ ], @rest[1...] for @rest[0]; } else { # We don't have to recurse further, so we # simply "return" @prev. take @prev; } }; # @prev: Empty array # @rest: @vals gather { helper [], @vals }; } my @a = outer [1,2,3],[4,5,6],[7,8,9]; say join "\n", @a; # 1 4 7 # 1 4 8 # [...] # 3 6 8 # 3 6 9
--Ingo
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Perl6 Contest #2: P6 That Doesn't Look Like P5
by dpuu (Chaplain) on Jun 03, 2005 at 21:45 UTC | |
Re^2: Perl6 Contest #2: P6 That Doesn't Look Like P5
by Limbic~Region (Chancellor) on Jun 03, 2005 at 19:50 UTC | |
by iblech (Friar) on Jun 03, 2005 at 20:24 UTC |