I've always steered clear of using prototypes but I thought I'd have a go with them to see if I could come up with a general routine a bit like List::MoreUtils::natatime() but taking a code block rather than returning an iterator. This is what I came up with, putting a prototype declaration at the top of the script and fleshing out the subroutine later.

... sub groupsOf (&$@); ... sub groupsOf (&$@) { my $rcToRun = shift; my $groupsOf = shift; my $rcDoIt; $rcDoIt = sub { $rcToRun->( map shift, 1 .. min scalar( @_ ), $groupsOf ), @_ ? &$rcDoIt : (); }; &$rcDoIt; } ...

It seems to do the right thing. With regard to rotating the elements of an array, I would have thought the push @array, shift @array for 1 .. $n; idiom was easier to understand but others may disagree. Here is sub groupsOf() in action.

use strict; use warnings; use feature qw{ say }; use List::Util qw{ min }; sub groupsOf (&$@); my @arr = ( 1 .. 20 ); say qq{@arr}; my @twos = groupsOf { reverse @_ } 2, @arr; say qq{@twos}; my @threes = groupsOf { reverse @_ } 3, @arr; say qq{@threes}; say q{-} x 40; my @rot1 = groupsOf { rotateFromFront( 2, @_ ) } 5, @arr; say qq{@rot1}; my @rot2 = groupsOf { rotateFromBack( 2, @_ ) } 5, @arr; say qq{@rot2}; say q{-} x 40; my @sums = groupsOf { my $sum; $sum += $_ for @_; $sum } 6, @arr; say qq{@sums}; sub groupsOf (&$@) { my $rcToRun = shift; my $groupsOf = shift; my $rcDoIt; $rcDoIt = sub { $rcToRun->( map shift, 1 .. min scalar( @_ ), $groupsOf ), @_ ? &$rcDoIt : (); }; &$rcDoIt; } sub rotateFromBack { my( $places, @arr ) = @_; unshift @arr, pop @arr for 1 .. $places; return @arr; } sub rotateFromFront { my( $places, @arr ) = @_; push @arr, shift @arr for 1 .. $places; return @arr; }

The output.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 1 4 3 6 5 8 7 10 9 12 11 14 13 16 15 18 17 20 19 3 2 1 6 5 4 9 8 7 12 11 10 15 14 13 18 17 16 20 19 ---------------------------------------- 3 4 5 1 2 8 9 10 6 7 13 14 15 11 12 18 19 20 16 17 4 5 1 2 3 9 10 6 7 8 14 15 11 12 13 19 20 16 17 18 ---------------------------------------- 21 57 93 39

I hope this is of interest.

Cheers,

JohnGG


In reply to Re^5: Switch the odd/even elements of an array by johngg
in thread Switch the odd/even elements of an array by jaredor

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.