All,
I was rather disappointed at the turnout of
Perl6 Contest: Test your Skills. After reading
Why aren't you using Perl 6 yet?, I realize there are a lot of good reasons. OTOH, silly exercises to demonstrate skill have never been frowned upon here, so I will continue to poke and prod people to show off their
Perl6 knowledge.
I finally figured out how to do arbitrarily nested loops, something tye figured out a long time ago. My problem is that I think in p5 so my p6 code as a result doesn't look very p6ish. The challenge is to achieve the same functionality as clearly and concisely as possible with an emphasis on p6 features.
use v6;
sub NestedLoop (++@loop, +%opt, +$code) returns Ref{
my @pos = 0 xx (@loop.elems - 1), -1;
return sub {
if ++@pos[-1] > @loop[-1].end {
for reverse 0 .. @pos.end - 1 -> $i {
next if @pos[$i] == @loop[$i].end;
++@pos[$i];
@pos = (@pos[0..$i], 0 xx (@pos.end - $i)) and last;
}
}
return () if @pos[-1] > @loop[-1].end;
return map -> $i { @loop[$i][@pos[$i]] } 0 .. @pos.end;
};
};
my $next = NestedLoop(loop => ([0..2], [0..2], [0..2]));
my @group;
while @group = $next() { say ~@group; }
Note: This code is functional but is currently lacking support for the optional parameters. They may be added later in a reply.
Update: See Pugs Examples for the latest
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.