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; }