| 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.08'; | |||||||||||||||||||
| 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', 'Class::Accessor'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 77 | 2 | __PACKAGE__->mk_accessors( | |||||||||||||||||||
| 78 | '_look_ahead', # list of computed values | ||||||||||||||||||||
| 79 | '_iterators', # list of iterators | ||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | sub new { | ||||||||||||||||||||
| 83 | 9 | ▶ | my($class, @items) = @_; | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 84 | 9 | my $self = bless { _look_ahead => [], _iterators => [] }, $class; | |||||||||||||||||||
| 85 | 9 | ▶ | $self->unget(@items) if @items; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | 9 | return $self; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | use overload ( | ||||||||||||||||||||
| 90 | 12221 | ▶ | '&{}' => sub { my($self) = @_; return sub { $self->next } }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 91 | 2 | '<>' => 'next', | |||||||||||||||||||
| 92 | '|' => 'filter', | ||||||||||||||||||||
| 93 | fallback => 1, | ||||||||||||||||||||
| 94 | 2 | ▶ | ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | #------------------------------------------------------------------------------ | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =head2 peek | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | Retrieves the Nth-element at the head of the stream, but keeps it in the | ||||||||||||||||||||
| 101 | stream to be retrieved by C<next()>. | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | Calls the iterator function to compute the items up to the Nth element, returns | ||||||||||||||||||||
| 104 | C<undef> if the stream is exhausted before reaching N. | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | As a special case, C<peek()> retrieves the element at the head of the stream, | ||||||||||||||||||||
| 107 | or C<undef> if the stream is empty. | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | #------------------------------------------------------------------------------ | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 54819 | ▶ | sub _is_iter { ref($_[0]) eq 'CODE' || is_iterator($_[0]) } | ||||||||||||||||||
|
Subroutine
_is_iter: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | sub peek { | ||||||||||||||||||||
| 116 | 52900 | ▶ | my($self, $n) = @_; | ||||||||||||||||||
|
Subroutine
peek: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||
| 117 | 52900 | ▶ | $n ||= 0; croak("negative index $n") if $n < 0; | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$n ||= 0
MC/DC100%1/1
$n ||= 0
$n
Truth table100%2/2
A
A= $n
| |||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 52899 | while (1) { | |||||||||||||||||||
| 120 | # return element if already computed | ||||||||||||||||||||
| 121 | 107753 | ▶ | return $self->_look_ahead->[$n] if $n < @{$self->_look_ahead}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | # empty list of iterators -> end of input | ||||||||||||||||||||
| 124 | 54931 | ▶ | return unless @{$self->_iterators}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | # get first iterator | ||||||||||||||||||||
| 127 | 54854 | my $iter = $self->_iterators->[0]; | |||||||||||||||||||
| 128 | 54854 | ▶ | if ( ! defined $iter ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 129 | 35 | shift @{$self->_iterators}; # skip undefined values | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | elsif ( _is_iter($iter) ) { | ||||||||||||||||||||
| 132 | 12204 | my $value = $iter->(); | |||||||||||||||||||
| 133 | 12204 | ▶ | if ( defined($value) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 134 | # allow an iterator to get another | ||||||||||||||||||||
| 135 | 12191 | unshift @{$self->_iterators}, $value; | |||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | else { | ||||||||||||||||||||
| 138 | 13 | shift @{$self->_iterators}; # exhausted | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | else { | ||||||||||||||||||||
| 142 | 42615 | push @{$self->_look_ahead}, $iter; # not iterator | |||||||||||||||||||
| 143 | 42615 | shift @{$self->_iterators}; | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | #------------------------------------------------------------------------------ | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =head2 next | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | Retrieves the element at the head of the stream and removes it from the stream. | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | Returns C<undef> if the stream is empty | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | =cut | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | #------------------------------------------------------------------------------ | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | sub next { | ||||||||||||||||||||
| 161 | 32665 | ▶ | my($self) = @_; | ||||||||||||||||||
|
Subroutine
next: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 162 | 32665 | $self->peek; # compute head element | |||||||||||||||||||
| 163 | 32665 | return shift @{$self->_look_ahead}; | |||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | #------------------------------------------------------------------------------ | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | =head2 unget | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | Pushes back a list of values and/or iterators to the stream, that will be | ||||||||||||||||||||
| 171 | retrieved on the subsequent calls to C<next()>. | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | Can be called from within an iterator, to insert values that will be returned | ||||||||||||||||||||
| 174 | before the current call, e.g. calling from the iterator: | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | $stream->unget(1..3); return 4; | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | will result in the values 4,1,2,3 being returned from the stream. | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | #------------------------------------------------------------------------------ | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | sub unget { | ||||||||||||||||||||
| 185 | 20455 | ▶ | my($self, @items) = @_; | ||||||||||||||||||
|
Subroutine
unget: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 186 | 20455 | unshift @{$self->_iterators}, @items, @{$self->_look_ahead}; | |||||||||||||||||||
| 187 | 20455 | @{$self->_look_ahead} = (); | |||||||||||||||||||
| 188 | } | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | #------------------------------------------------------------------------------ | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =head1 EXPORT | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | None. | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | =head1 AUTHOR | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | Paulo Custodio, C<< <pscust at cpan.org> >> | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | =head1 BUGS and FEEDBACK | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | Please report any bugs or feature requests through the web interface at | ||||||||||||||||||||
| 203 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Iterator-Simple-Lookahead>. | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | Inspired in L<HOP::Stream|HOP::Stream> and L<Iterator::Simple|Iterator::Simple>. | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | Copyright (C) 2013 by Paulo Custodio | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||
| 214 | it under the same terms as Perl itself, either Perl version 5.16.1 or, | ||||||||||||||||||||
| 215 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | =cut | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | 2 | 1; | |||||||||||||||||||