blib/lib/Iterator/Simple/Lookahead.pm

filter: Statementstmt 100% 65 / 65 Branchbran 100% 16 / 16 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 100% 102 / 102 SCAR 23.0
CC
10
Cov
100%
peek
8.0
new
2.0
_is_iter
2.0
CRAP 10.0
↑ Summary
1 # $Id: Lookahead.pm,v 1.5 2013/07/26 23:25:07 Paulo Exp $
2
3 package Iterator::Simple::Lookahead;
4
5 #------------------------------------------------------------------------------
6
7 =head1 NAME
8
9 Iterator::Simple::Lookahead - Simple iterator with lookahead and unget
10
11 =cut
12
13 #------------------------------------------------------------------------------
14
15 2 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Iterator::Simple qw( is_iterator );
Subroutine BEGIN: called CC=1 SCAR=0.0
21
22 2 our $VERSION = '0.07';
23
24 #------------------------------------------------------------------------------
25
26 =head1 SYNOPSIS
27
28 use Iterator::Simple::Lookahead;
29 my $iter = Iterator::Simple::Lookahead->new( sub {}, @values );
30 my $first = $iter->peek();
31 my $second = $iter->peek(1);
32 my $next = $iter->next; # or $iter->()
33 $iter->unget( sub {}, @values );
34
35 =head1 DESCRIPTION
36
37 This module encapsulates an iterator function. An iterator function returns the
38 next element in the stream on each call, and returns C<undef> on end of input.
39
40 The iterator can return a code reference - this new iterator is inserted at the
41 head of the queue.
42
43 The object allows the user to C<peek()> the Nth element of the stream without
44 consuming it, or to get it and remove from the stream.
45
46 A list of items can also be pushed back to the stream by C<unget()>,
47 to be retrieved in the subsequent C<next()> calls. The next item can also be
48 retrieved by calling C<$iter-E<gt>()>.
49
50 The input list to the constructor and to C<unget()> contains items to be
51 retrieved on each C<next()> call, or code references to be called to extract the
52 next item from the stream.
53
54 Other types of input can be converted to a code reference by C<iter()> of
55 L<Iterator::Simple|Iterator::Simple>.
56
57 This module is built on top of L<Iterator::Simple|Iterator::Simple>
58 and returns iterators that are compatible with the former,
59 i.e. an object of type C<Iterator::Simple::Lookahead>
60 can be used as an input to C<iter()>.
61
62 =head1 FUNCTIONS
63
64 =head2 new
65
66 Creates a new object ready to retrieve elements from the given input list. The
67 list contains either values to be returned in a subsequent C<next()> call, or
68 code references to be called to extract the next item from the stream.
69
70 Other types of input can be converted to a code reference by C<iter()> of
71 L<Iterator::Simple|Iterator::Simple>.
72
73 =cut
74
75 #------------------------------------------------------------------------------
76 2 ▶use base 'Iterator::Simple::Iterator';
Subroutine BEGIN: called CC=1 SCAR=0.0
77 use Class::XSAccessor {
78 2 accessors => [
79 '_look_ahead', # list of computed values
80 '_iterators', # list of iterators
81 ],
82 2 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
83
84 sub new {
85 9 ▶ my($class, @items) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
86 9 my $self = bless { _look_ahead => [], _iterators => [] }, $class;
87 9 ▶ $self->unget(@items) if @items;
Branch: 1 branch
BranchTrueFalseTotal
if @items 8 1 9
88 9 return $self;
89 }
90
91 use overload (
92 12221 ▶ '&{}' => sub { my($self) = @_; return sub { $self->next } },
Subroutine __ANON__: called CC=1 SCAR=0.0
93 2 '<>' => 'next',
94 '|' => 'filter',
95 fallback => 1,
96 2 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
97
98 #------------------------------------------------------------------------------
99
100 =head2 peek
101
102 Retrieves the Nth-element at the head of the stream, but keeps it in the
103 stream to be retrieved by C<next()>.
104
105 Calls the iterator function to compute the items up to the Nth element, returns
106 C<undef> if the stream is exhausted before reaching N.
107
108 As a special case, C<peek()> retrieves the element at the head of the stream,
109 or C<undef> if the stream is empty.
110
111 =cut
112
113 #------------------------------------------------------------------------------
114
115 54819 ▶sub _is_iter { ref($_[0]) eq 'CODE' || is_iterator($_[0]) }
Subroutine _is_iter: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "CODE" or is_iterator($_[0]) 42624 12195 54819
116
117 sub peek {
118 52900 ▶ my($self, $n) = @_;
Subroutine peek: called CC=8 SCAR=20.8
Pod documented
119 52900 ▶ $n ||= 0; croak("negative index $n") if $n < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 0 1 52899 52900
Condition100%2/2
$n ||= 0
l!l
1001242888
MC/DC100%1/1
$n ||= 0
$n
Truth table100%2/2
A
A= $n
Aresult
11
00
120
121 52899 while (1) {
122 # return element if already computed
123 107753 ▶ return $self->_look_ahead->[$n] if $n < @{$self->_look_ahead};
Branch: 1 branch
BranchTrueFalseTotal
if $n < @{$self->_look_ahead;} 52822 54931 107753
124
125 # empty list of iterators -> end of input
126 54931 ▶ return unless @{$self->_iterators};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_iterators;} 77 54854 54931
127
128 # get first iterator
129 54854 my $iter = $self->_iterators->[0];
130 54854 ▶ if ( ! defined $iter ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $iter) { } 35 54819 54854
elsif (_is_iter($iter)) { } 12204 42615 54819
131 35 shift @{$self->_iterators}; # skip undefined values
132 }
133 elsif ( _is_iter($iter) ) {
134 12204 my $value = $iter->();
135 12204 ▶ if ( defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 12191 13 12204
136 # allow an iterator to get another
137 12191 unshift @{$self->_iterators}, $value;
138 }
139 else {
140 13 shift @{$self->_iterators}; # exhausted
141 }
142 }
143 else {
144 42615 push @{$self->_look_ahead}, $iter; # not iterator
145 42615 shift @{$self->_iterators};
146 }
147 }
148 }
149
150 #------------------------------------------------------------------------------
151
152 =head2 next
153
154 Retrieves the element at the head of the stream and removes it from the stream.
155
156 Returns C<undef> if the stream is empty
157
158 =cut
159
160 #------------------------------------------------------------------------------
161
162 sub next {
163 32665 ▶ my($self) = @_;
Subroutine next: called CC=1 SCAR=0.0
Pod documented
164 32665 $self->peek; # compute head element
165 32665 return shift @{$self->_look_ahead};
166 }
167
168 #------------------------------------------------------------------------------
169
170 =head2 unget
171
172 Pushes back a list of values and/or iterators to the stream, that will be
173 retrieved on the subsequent calls to C<next()>.
174
175 Can be called from within an iterator, to insert values that will be returned
176 before the current call, e.g. calling from the iterator:
177
178 $stream->unget(1..3); return 4;
179
180 will result in the values 4,1,2,3 being returned from the stream.
181
182 =cut
183
184 #------------------------------------------------------------------------------
185
186 sub unget {
187 20455 ▶ my($self, @items) = @_;
Subroutine unget: called CC=1 SCAR=0.0
Pod documented
188 20455 unshift @{$self->_iterators}, @items, @{$self->_look_ahead};
189 20455 @{$self->_look_ahead} = ();
190 }
191
192 #------------------------------------------------------------------------------
193
194 =head1 EXPORT
195
196 None.
197
198 =head1 AUTHOR
199
200 Paulo Custodio, C<< <pscust at cpan.org> >>
201
202 =head1 BUGS and FEEDBACK
203
204 Please report any bugs or feature requests through the web interface at
205 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Iterator-Simple-Lookahead>.
206
207 =head1 ACKNOWLEDGEMENTS
208
209 Inspired in L<HOP::Stream|HOP::Stream> and L<Iterator::Simple|Iterator::Simple>.
210
211 =head1 COPYRIGHT AND LICENSE
212
213 Copyright (C) 2013 by Paulo Custodio
214
215 This library is free software; you can redistribute it and/or modify
216 it under the same terms as Perl itself, either Perl version 5.16.1 or,
217 at your option, any later version of Perl 5 you may have available.
218
219 =cut
220
221 2 1;
↑ Summary