-
-
03 Mar 2019 12:06:01 UTC
- Distribution: Iterator-Simple-Lookahead
- Module version: 0.09
- Source (raw)
- Browse (raw)
- Changes
- How to Contribute
- Issues (0)
- Testers (719 / 0 / 0)
- Kwalitee
Bus factor: 0- 100.00% Coverage
- License: perl_5
- Perl: v5.8.0
- Activity
24 month- Tools
- Download (6.34KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
- Dependencies
- Class::Accessor
- Iterator::Simple
- Test::More
- and possibly others
- Reverse dependencies
- CPAN Testers List
- Dependency graph
- NAME
- SYNOPSIS
- DESCRIPTION
- FUNCTIONS
- EXPORT
- AUTHOR
- BUGS and FEEDBACK
- ACKNOWLEDGEMENTS
- COPYRIGHT AND LICENSE
NAME
Iterator::Simple::Lookahead - Simple iterator with lookahead and unget
SYNOPSIS
use Iterator::Simple::Lookahead; my $iter = Iterator::Simple::Lookahead->new( sub {}, @values ); my $first = $iter->peek(); my $second = $iter->peek(1); my $next = $iter->next; # or $iter->() $iter->unget( sub {}, @values );
DESCRIPTION
This module encapsulates an iterator function. An iterator function returns the next element in the stream on each call, and returns
undef
on end of input.The iterator can return a code reference - this new iterator is inserted at the head of the queue.
The object allows the user to
peek()
the Nth element of the stream without consuming it, or to get it and remove from the stream.A list of items can also be pushed back to the stream by
unget()
, to be retrieved in the subsequentnext()
calls. The next item can also be retrieved by calling$iter->()
.The input list to the constructor and to
unget()
contains items to be retrieved on eachnext()
call, or code references to be called to extract the next item from the stream.Other types of input can be converted to a code reference by
iter()
of Iterator::Simple.This module is built on top of Iterator::Simple and returns iterators that are compatible with the former, i.e. an object of type
Iterator::Simple::Lookahead
can be used as an input toiter()
.FUNCTIONS
new
Creates a new object ready to retrieve elements from the given input list. The list contains either values to be returned in a subsequent
next()
call, or code references to be called to extract the next item from the stream.Other types of input can be converted to a code reference by
iter()
of Iterator::Simple.peek
Retrieves the Nth-element at the head of the stream, but keeps it in the stream to be retrieved by
next()
.Calls the iterator function to compute the items up to the Nth element, returns
undef
if the stream is exhausted before reaching N.As a special case,
peek()
retrieves the element at the head of the stream, orundef
if the stream is empty.next
Retrieves the element at the head of the stream and removes it from the stream.
Returns
undef
if the stream is emptyunget
Pushes back a list of values and/or iterators to the stream, that will be retrieved on the subsequent calls to
next()
.Can be called from within an iterator, to insert values that will be returned before the current call, e.g. calling from the iterator:
$stream->unget(1..3); return 4;
will result in the values 4,1,2,3 being returned from the stream.
EXPORT
None.
AUTHOR
Paulo Custodio,
<pscust at cpan.org>
BUGS and FEEDBACK
Please report any bugs or feature requests through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Iterator-Simple-Lookahead.
ACKNOWLEDGEMENTS
Inspired in HOP::Stream and Iterator::Simple.
COPYRIGHT AND LICENSE
Copyright (C) 2013 by Paulo Custodio
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.16.1 or, at your option, any later version of Perl 5 you may have available.
Module Install Instructions
To install Iterator::Simple::Lookahead, copy and paste the appropriate command in to your terminal.
cpanm Iterator::Simple::Lookahead
perl -MCPAN -e shell install Iterator::Simple::Lookahead
For more information on module installation, please visit the detailed CPAN module installation guide.