Just to get the party started, and without putting much effort into it, you can change this:
next if ! grep { $_<val> == 5 } @combo;
to:
next if none(@a) == 5;
Update: another quick note. Your iterator should be able to be nicely implemented in terms of gather/take. Pugs doesn't have it yet, so I haven't had a chance to play with it enough to be very comfortable, but it would be nice if someone could show how to do that. :-)
Another update: here's my crack at gatherifying your combo generator. I dunno if it's right, but it fits my understanding of how the construct is supposed to work:
sub combo (Int $by is copy, @list is copy) returns Ref { my @position = 0 .. ($by - 2), $by - 2; my @stop = @list.elems - $by .. @list.end; my $done = undef; gather { until $done { my $cur = @position.end; while ++@position[$cur] > @stop[$cur] { --$cur; @position[$cur]++; next if @position[$cur] > @stop[$cur]; my $new_pos = @position[$cur]; @position[$cur .. @position.end] = $new_pos .. ($new_p +os + $by); last; } $done = 1 if @position[0] == @stop[0]; take [ @list[@position] ]; } } }
As we can see, this was a very straightforward change. My understanding is that this should act lazilly -- possibly as a coroutine -- if used in a lazy context. Instead of returning lists, I have it take array references, because I'm not sure if taking a list would Do The Right Thing. Again, this is just my understanding of the feature. I could have this all wrong.
Update on another update: added until loop around body of gather. I think it's correct this time. :-)
In reply to Re: Perl6 Contest: Test your Skills
by revdiablo
in thread Perl6 Contest: Test your Skills
by Limbic~Region
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |