dragonchild,
If you can figure out the proper lazy gather/take approach to turn your recursive function into an iterative one, I would be happy to use it. Until then, I am sticking with iterative translation from p5.
use v6; # Brute force proof that every cribbage hand with a 5 is >= 2 points # See http://perlmonks.org/index.pl?node_id=458728 for details my $next = combo(5, new_deck()); while my @combo = $next() { # Skip all hands that do not contain a 5 next if none( @combo>>.<val> ) == 5; # Skip all hands that have a score of at least 2 next if score( @combo ) > 1; # Print out the rest say ~@combo>>.<suit>; } sub score ( @hand ) returns Int { my $score = 0; # [234] of a kind my %ordval; for @hand>>.<num> { %ordval{$_}++ }; for %ordval.values { $score += $_ * $_ - 1 } # Flush $score += ([eq] @hand[0..3]>>.<suit>) ?? ([eq] @hand[3,4]>>.<suit>) ?? 5 :: 4 :: 0; # Check for right-jack, @hand[-1] is community card $score++ if grep { $_<num> == 11 && $_<suit> eq @hand[-1]<suit> } +@hand[0..3]; # Count 15's my @vals = @hand>>.<val>; for 2 .. 5 { my $next = combo($_, @vals); while my @combo = $next() { $score += 2 if ([+] @combo) == 15 +} } # Runs SPAN: for 5, 4, 3 -> $span { for sort { $^a <=> $^b } %ordval.keys -> $start { if all( %ordval{$start .. $start + $span} ) > 1 { $score += [*] %ordval{$start .. $start + $span}, $span +; last SPAN; } } } return $score; } 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; return sub { return () if $done; my $cur = @position.end; while ++@position[ $cur ] > @stop[ $cur ] { @position[ --$cur ]++; next if @position[ $cur ] > @stop[ $cur ]; my $new_pos = @position[ $cur ]; @position[ $cur .. @position.end ] = $new_pos .. $new_pos ++ $by; last; } $done = 1 if @position[ 0 ] == @stop[ 0 ]; return @list[ @position ]; }; } sub new_deck () returns Array { return map -> $num { map -> $suit { { num => $num, val => $num > 10 ?? 10 :: $num, suit => $su +it } } <H D C S>; } 1..13; }
This node is subject to change if I find that I have improper syntax or if there is a more perl6ish way of doing something. In the near future, I will be adding this as a Pugs Example.

Cheers - L~R


In reply to Re^4: Perl6 Contest: Test your Skills by Limbic~Region
in thread Perl6 Contest: Test your Skills by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.