Inspired by a message over on the Applescript users list, I thought
I'd try to whip up a silly little P6 script to print out the 12 days
of Christmas.  I tried to use as many P6isms as I could squeeze in,
but wanted it to work in Rakudo, which doesn't yet parse closure
traits or multimethods, so had to scale back some.

What's the consensus on how to do an idiomatic countdown loop?  I used
for [1..$n].reverse...

I also tried this, but it caused Rakudo to throw a StopIteration and
then segfault:

for [...@gifts[0..$day-1]].pairs.reverse -> $n, $g

Anyway, here's my take:

#!./perl6
my @gifts = ('partridge in a pear tree', 'turtle doves', 'French hens',
             'calling birds', 'gold rings', 'geese a-laying',
             'swans a-swimming', 'maids a-milking', 'ladies dancing',
             'lords a-leaping', 'pipers piping', 'drummers drumming');

for (1..12) -> $day
{
    say "On the {nth($day)} day of Christmas, my true love gave to me:";
    for [0..$day-1].reverse -> $n
    {
        my $g = @gifts[$n];
        print "\t";
        say $n ?? sprintf '%2d %s%s', $n+1, $g, ($day > 2 ?? ',' !! '')
               !! "{$day > 1 ?? ' and' !! ''} a $g.";
    }
}

multi nth($x where { $^x % 10 == 1 && $^x % 100 != 11} ) { "{$x}st" };
multi nth($x where { $^x % 10 == 2 && $^x % 100 != 12} ) { "{$x}nd" };
multi nth($x where { $^x % 10 == 3 && $^x % 100 != 13} ) { "{$x}rd" };
multi nth($x) { "{$x}th" };

And as it's just past midnight here in US/Eastern, Merry Christmas to
those who celebrate it!

-- 
Mark J. Reed <markjr...@gmail.com>

Reply via email to