in reply to Perl6 Contest #2: P6 That Doesn't Look Like P5
It's a slightly functional way of doing it. (Assuming my understanding of lexically-scoped subs is correct) I set up a recursive "incrementer" function which increments one position of @pos and "carries over" via a recursive call. It's tail-recursive, so if the compiler is smart, we don't lose a lot by making it recursive instead of iterative.
From what I can tell, there are two things currently holding back pugs:sub NestedLoops (*@loop) returns Ref { my @pos = 0 xx (@loop.elems - 1), -1; my sub incr($i) { if ( ++@pos[$i] == @loop[$i].elems ) { @pos[$i] = 0; return $i ?? incr($i - 1) :: 0; } return 1; }; return sub { incr(@loop.end) or return; zip(@loop, @pos) ==> map -> $a, $i { $a[$i] }; }; }; my $iter = NestedLoops( [0..2], [0..2] ); my @group; while ( @group = $iter() ) { say ~@group; }
But pugs didn't like that..@loops >>.[]<< @pos
blokhead
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Perl6 Contest #2: P6 That Doesn't Look Like P5
by kelan (Deacon) on Jun 03, 2005 at 13:09 UTC | |
by iblech (Friar) on Jun 03, 2005 at 18:18 UTC | |
Re^2: Perl6 Contest #2: P6 That Doesn't Look Like P5
by kelan (Deacon) on Jun 03, 2005 at 18:39 UTC |