From: Florian Ragwitz Date: Mon, 1 Feb 2010 02:52:18 +0000 (+0100) Subject: Add Array->each_n. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=aaddca9e197db971de384beaf4d15e398be06f7f;p=gitmo%2FMoose-Autobox.git Add Array->each_n. --- diff --git a/Makefile.PL b/Makefile.PL index e14ec21..cde3758 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -10,7 +10,7 @@ requires 'autobox' => '2.23'; requires 'Moose' => '0.42'; requires 'Perl6::Junction' => '1.40000'; -test_requires 'Test::More' => '0.62'; +test_requires 'Test::More' => '0.89'; test_requires 'Test::Exception' => '0.21'; auto_manifest; diff --git a/lib/Moose/Autobox/Array.pm b/lib/Moose/Autobox/Array.pm index e43c893..1221e17 100644 --- a/lib/Moose/Autobox/Array.pm +++ b/lib/Moose/Autobox/Array.pm @@ -134,6 +134,17 @@ sub each_value { $sub->($_) for @$array; } +sub each_n_values { + my ($array, $n, $sub) = @_; + my $it = List::MoreUtils::natatime($n, @$array); + + while (my @vals = $it->()) { + $sub->(@vals); + } + + return; +} + # end indexed sub flatten { @@ -235,6 +246,8 @@ This is a role to describe operations on the Array type. =item B +=item B + =back =head2 Indexed implementation diff --git a/t/011_each_n_values.t b/t/011_each_n_values.t new file mode 100644 index 0000000..4f9dc29 --- /dev/null +++ b/t/011_each_n_values.t @@ -0,0 +1,29 @@ +use strict; +use warnings; +use Test::More; + +use Moose::Autobox; + +{ + my @array = ('a' .. 'z'); + + my $aref = [ @array ]; + + { + my @vals; + @array->each_n_values(2, sub { push @vals, [@_] }); + is(scalar @vals, 13); + is(scalar @$_, 2) for @vals; + is_deeply(@vals->map(sub { @{ $_ } }), [@array]); + } + + { + my @vals; + $aref->each_n_values(2, sub { push @vals, [@_] }); + is(scalar @vals, 13); + is(scalar @$_, 2) for @vals; + is_deeply(@vals->map(sub { @{ $_ } }), $aref); + } +} + +done_testing;