Add Array->each_n.
[gitmo/Moose-Autobox.git] / lib / Moose / Autobox / Array.pm
index 21ce08a..ab28fbf 100644 (file)
@@ -3,7 +3,7 @@ use Moose::Role 'with';
 use Perl6::Junction;
 use Moose::Autobox;
 
-our $VERSION = '0.09';
+our $VERSION = '0.10';
 
 with 'Moose::Autobox::Ref',
      'Moose::Autobox::List',
@@ -126,9 +126,7 @@ sub each {
 
 sub each_key {
     my ($array, $sub) = @_;
-    for my $i (0 .. $#$array) {
-      $sub->($i);
-    }
+    $sub->($_) for (0 .. $#$array);
 }
 
 sub each_value {
@@ -136,6 +134,17 @@ sub each_value {
     $sub->($_) for @$array;
 }
 
+sub each_n {
+    my ($array, $n, $sub) = @_;
+    my $it = List::MoreUtils::natatime($n, @$array);
+
+    while (my @vals = $it->()) {
+        $sub->(@vals);
+    }
+
+    return;
+}
+
 # end indexed
 
 sub flatten {
@@ -237,6 +246,8 @@ This is a role to describe operations on the Array type.
 
 =item B<flatten_deep ($depth)>
 
+=item B<each_n ($n, $callback)>
+
 =back
 
 =head2 Indexed implementation
@@ -255,6 +266,12 @@ This is a role to describe operations on the Array type.
 
 =item B<kv>
 
+=item B<each>
+
+=item B<each_key>
+
+=item B<each_value>
+
 =back
 
 =head2 List implementation