NAME
Lazy::Iterator - Objects encapsulating a set of lazy evaluation functions.
VERSION
version 0.003
SYNOPSIS
use Lazy::Iterator;
my $lazy = Lazy::Iterator->new(sub { state $i++ });
while (my $next = $lazy->get()) { print "$next\n"; sleep 1; }
DESCRIPTION
Objects encapsulating a set of lazy evaluation functions, meaning you can combine them using the l_*
functions from Lazy::Util
.
METHODS
Lazy::Iterator->new($source)
my $lazy = Lazy::Iterator->new(sub { $i++ });
Lazy::Iterator->new
takes a code reference which will be used as the source for all the values and returns a Lazy::Iterator
object encapsulating that source.
The $source
needs to be either a CODE
reference, or a Scalar::Defer
variable of type 0
, provided you have Scalar::Defer
available.
$lazy->exhausted()
my $exhausted = $lazy->exhausted();
$lazy->exhausted()
checks if there's any more values left in the source, and caches any such value for the next $lazy->get()
call. It returns 0 if there are values left, and 1 if the source is exhausted.
An exhausted Lazy::Iterator
object will always return undef
from a $lazy->get()
call.
$lazy->get()
my $next = $lazy->get();
$lazy->get
returns the next value from the source it encapsulates. When there are no more values it returns undef
.
$lazy->get_all()
my @crazy = $lazy->get_all();
$lazy->get_all
returns all the values from the source, if it can. This has the potential to never return as well as running out of memory if given a source of infinite values.
NOTES
If Scalar::Defer is installed, it will assume that any variable of type 0
is a Scalar::Defer
variable and will treat it as a source of values.
SEE ALSO
AUTHOR
Andreas Guldstrand <andreas.guldstrand@gmail.com>
COPYRIGHT AND LICENSE
This software is Copyright (c) 2017 by Andreas Guldstrand.
This is free software, licensed under:
The MIT (X11) License