File Coverage

blib/lib/Seq/Iter.pm
Criterion Covered Total %
statement 28 29 96.5
branch 7 10 70.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 42 46 91.3


line stmt bran cond sub pod time code
1             package Seq::Iter;
2              
3 2     2   529541 use 5.010001;
  2         9  
4 2     2   15 use strict;
  2         13  
  2         167  
5 2     2   16 use warnings;
  2         4  
  2         210  
6              
7 2     2   16 use Exporter qw(import);
  2         4  
  2         768  
8              
9             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10             our $DATE = '2023-11-21'; # DATE
11             our $DIST = 'Seq-Iter'; # DIST
12             our $VERSION = '0.001'; # VERSION
13              
14             our @EXPORT_OK = qw(seq_iter);
15              
16             our $BUFFER_SIZE = 10;
17              
18             sub seq_iter {
19 1     1 1 474022 my @orig_seq = @_;
20              
21 1         3 my $index = -1;
22 1         6 my $index_coderef;
23             my @gen_seq;
24             sub {
25 6     6   29 $index++;
26 6 50       16 splice @gen_seq, $BUFFER_SIZE-1 if @gen_seq > $BUFFER_SIZE;
27              
28             RETRY:
29 7 100       33 if (defined $index_coderef) {
    50          
30 4         11 my $item = $orig_seq[$index_coderef]->($index, \@orig_seq, \@gen_seq);
31 4 50       32 return unless defined $item;
32 4         7 unshift @gen_seq, $item;
33 4         9 return $item;
34             } elsif ($index >= @orig_seq) {
35 0         0 return;
36             } else {
37 3         7 my $item = $orig_seq[$index];
38 3 100       10 if (ref $item eq 'CODE') {
39 1         2 $index_coderef = $index;
40 1         11 goto RETRY;
41             } else {
42 2         5 unshift @gen_seq, $item;
43 2         6 return $item;
44             }
45             }
46 1         9 };
47             }
48              
49             1;
50             # ABSTRACT: Generate a coderef iterator from a sequence of items, the last of which can be a coderef to produce more items
51              
52             __END__