Here are my solutions to Problem 9. The first is concise and looks fairly efficient. The second is less straightforward; I wanted to solve it with one big map, in the hopes of allowing it to work lazily. I don't know how to tell if I succeeded, though.

Code tested in Pugs:

sub p09_group_Util_1 (*@list) { my @ret; for @list -> $val { push @ret, [] if !@ret or $val !~~ @ret[-1][-1]; push @ret[-1], $val; } return @ret; } sub p09_group_Util_2 (*@list) { my @tmp; return map -> $index, $val { my @ret; if !@tmp or $val !~~ @tmp[-1] { @ret = [ @tmp ] if @tmp; undefine @tmp; } push @tmp, $val; push @ret, [ @tmp ] if @tmp and $index == @list.end; @ret; # May contain 0, 1, or 2 arrayrefs. }, @list.kv; } my @data = <a a a a b c c a a d e e e e>; say '9.1 ', p09_group_Util_1(@data).perl; say '9.2 ', p09_group_Util_2(@data).perl; # When dumped via .perl, Util_1 is wrapped in [], and Util_2 in (). # When dumped via .yaml, Util_1 and Util_2 are identical. Bug?
Output:
9.1 [["a", "a", "a", "a"], ["b",], ["c", "c"], ["a", "a"], ["d",], ["e +", "e", "e", "e"]] 9.2 (["a", "a", "a", "a"], ["b",], ["c", "c"], ["a", "a"], ["d",], ["e +", "e", "e", "e"])


In reply to Re: 99 Problems in Perl6 by Util
in thread 99 Problems in Perl6 by Ovid

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.