in reply to Re: Perl6 Contest #2: P6 That Doesn't Look Like P5
in thread Perl6 Contest #2: P6 That Doesn't Look Like P5

Here's a version that has the $only_when and $code functionality. It's not quite what I originally aimed at because I came across at least one (and possibly more than one) Pugs bug. In particular, my next task is to write up a test case for the bug that causes the one line version of the following test from NestedLoop to die with a casting error in pugs if $only_when happens to be undef:
return &?SUB() if $only_when and not $only_when(@next);
Here's the code as it currently stands:
#!/usr/bin/pugs use v6; sub NestedLoop (++@loop, +$only_when, +$code) { my &iter = NL2(loop => @loop); sub { my @next = iter; return @next unless defined @next[0]; if $only_when { return &?SUB() unless $only_when(@next); } $code(@next) if $code; return @next; } } sub NL2 (++@loop) { coro { given (@loop.elems) { when 0 { yield [] } when 1 { for @loop[0] { yield [$^first] } yield undef whi +le 1 } default { for @loop[0] -> $first { my &rest = NL2(loop => @loop[1..Inf]); my @rest; while @rest = rest() { yield [$first, @rest]; } } yield undef while 1; } } } } my ($cnt, $item); my &iter = NestedLoop(loop => ([0..2], [3..5], [6..8]), only_when => sub { ++$cnt % 2 }, code => sub {say "reversed: {reverse @^grou +p}"}); say "ITER {$cnt}: {$item.perl}" while $item = iter;

Replies are listed 'Best First'.
Re^3: Perl6 Contest #2: P6 That Doesn't Look Like P5
by geoffb (Novice) on Jun 03, 2005 at 03:15 UTC
    I just added a test to pugs for the undef cast fail bug above (my first commit!), so hopefully this will be a non-issue soon.