Or in Perl 6:
sub groups_of_n ($n,*@a) { return map { [ @a[$_ + ^$n]:v ] }, ^@a:by($n); }
This factors out the multiplications by using a step of $n on the initial range. We've also made use of unary ^, which is short for 0..^, that is, "all the numbers up to but not including this number." Unfortunately pugs does not yet implement :v for existing values or :by for range stepping, and we only just changed the semantics of ranges to allow scalar operators to return modified ranges, so that's not implemented yet either. But here's a version that works in pugs today:
sub groups_of_n ($n,*@a) { return map { [ @a[$_ ..^ min($_+$n,+@a)]] }, (0..@a.end/$n) »*« $n; }
This also factors out all the multiplications, but this time by hyper multiplying the range we feed to map. Inside the subscript it makes use of the ..^ notation to exclude the endpoint without having to subtract 1. Note that min is a built-in in Perl 6. Also note that map now requires a comma after the block, one of many grammar regularizations in Perl 6.

Actually, there are a couple things that will break in that last example, because min is turning into an infix, and the listop form would have to be written as a reduce operator. Also, dwimmy hypers require you to aim the small end of the French quote at the argument to be dwimmed, so once pugs catches up with those spec changes you'd probably end up writing that last example as:

sub groups_of_n ($n,*@a) { return map { [ @a[$_ ..^ ($_+$n min +@a)]] }, (0..@a.end/$n) »*» $n; }

In reply to Re^4: Syntactically cool list of lists by TimToady
in thread Syntactically cool list of lists by jettero

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.