Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

implement inatatime: return n elements at a time from an interable value #1

Merged
merged 1 commit into from

2 participants

@pcmantz

I've found myself re-implementing this function many times while working with Iterator::Simple. Are you planning on uploading this module to the CPAN? It would be very useful.

@ray1729
Owner
@ray1729 ray1729 merged commit b3b6f70 into ray1729:master
@ray1729
Owner

Hi,

I (eventually) got round to releasing Iterator::Simple::Util to CPAN, you should find it there as soon as the indexer has done its stuff. Sorry I took so long about it.

Ray.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 26 additions and 2 deletions.
  1. +18 −0 lib/Iterator/Simple/Util.pm
  2. +8 −2 t/iterator-simple-util.t
View
18 lib/Iterator/Simple/Util.pm
@@ -9,6 +9,7 @@ use Sub::Exporter -setup => {
iany inone inotall
ifirstval ilastval
ibefore ibefore_incl iafter iafter_incl
+ inatatime
)
]
};
@@ -308,6 +309,23 @@ sub iafter_incl (&$) {
return ichain iter( [$_] ), $iter;
}
+sub inatatime ($;$) {
+ my ($kicks, $iter) = @_;
+
+ $iter = iter $iter;
+
+ return iterator {
+ my @vals;
+
+ for (1 .. $kicks) {
+ my $val = $iter->next;
+ last unless defined $val;
+ push @vals, $val;
+ }
+ return @vals ? \@vals : undef;
+ };
+}
+
sub _ensure_coderef {
unless( ref( shift ) eq 'CODE' ) {
require Carp;
View
10 t/iterator-simple-util.t
@@ -13,7 +13,8 @@ BEGIN {
imax imin imaxstr iminstr imax_by imin_by imaxstr_by iminstr_by
iany inone inotall
ifirstval ilastval
- ibefore ibefore_incl iafter iafter_incl )
+ ibefore ibefore_incl iafter iafter_incl
+ inatatime )
);
}
@@ -193,6 +194,11 @@ note "Testing iafter_incl";
is_deeply $it->to_array, [3..10];
}
+note "Testing inatatime";
+{
+ ok my $it = inatatime 3, iter [0..10];
+ isa_ok $it, Iterator::Simple->ITERATOR_CLASS;
+ is_deeply $it->to_array, [ [0..2], [3..5], [6..8], [9,10] ];
+}
-
done_testing();
Something went wrong with that request. Please try again.