Data-Enumerable-Lazy
view release on metacpan or search on metacpan
accumulator $acc provided as the 1st argument. `$callback' should always
return the new state of `$acc'.
`reduce()' is defined for finite enumerables only.
Example
Data::Enumerable::Lazy
->from_array(1, 2, 3)
->reduce(1, sub {
my ($acc, $number) = @_;
return $acc *= $number
});
grep($callback, $max_lookahead)
`grep()' is a function which returns a new enumerable by applying a
user-defined filter function.
`grep()' might be applied to both finite and infinite enumerables. In
case of an infinitive enumerable there is an additional argument
specifying max number of lookahead steps. If an element satisfying the
condition could not be found in `max_lookahead' steps, an enumerable is
considered to be completely iterated and `has_next()' will return false.
`grep()' returns a new enumerable with quite special properties:
`has_next()' will perform a look ahead and call the original enumerable
`next()' method in order to find an element for which the user-defined
function will return true. `next()', on the other side, returns the
value that was pre-fetched by `has_next()'.
Example
Data::Enumerable::Lazy
->from_list(1, 2, 3)
->grep(sub {
my ($number) = @_;
return $number % 2
});
resolve()
Resolves an enumerable completely. Applicable for finite enumerables
only. The method returns nothing.
take($N_elements)
Resolves first $N_elements and returns the resulting list. If there are
fewer than N elements in the enumerable, the entire enumerable would be
returned as a list.
take_while($callback)
This function takes elements until it meets the first one that does not
satisfy the conditional callback. The callback takes only 1 argument: an
element. It should return true if the element should be taken. Once it
returned false, the stream is over.
continue($ext = %{ on_next => sub {}, ... })
Creates a new enumerable by extending the existing one. on_next is the
only manfatory argument. on_has_next might be overriden if some custom
logic comes into play.
is_finite is inherited from the parent enumerable by default. All
additional attributes would be transparently passed to the constuctor.
count()
Counts the number of the elements in the stream. This method iterates
through the stream so it makes it exhausted by the end of the
computatuion.
yield($result)
This method is supposed to be called from `on_next' callback only. This
is the only valid result for an Enumerable to return the next step
result. Effectively, it ensures the returned result conforms to the
required interface and is wrapped in a lazy wrapper if needed.
CLASS METHODS
empty()
Returns an empty enumerable. Effectively it means an equivalent of an
empty array. `has_next()' will return false and `next()' will return
undef. Useful whenever a `on_next()' step wants to return an empty
resultset.
singular($val)
Returns an enumerable with a single element $val. Actively used as an
internal data container.
from_list(@list)
Returns a new enumerable instantiated from a list. The easiest way to
initialize an enumerable. In fact, all elements are already resolved so
this method sets `is_finite=1' by default.
cycle()
Creates an infinitive enumerable by cycling the original list. E.g. if
the original list is [1, 2, 3], `cycle()' will generate an infinitive
sequences like: 1, 2, 3, 1, 2, 3, 1, ...
infinity()
Returns a new infinite enumerable. `has_next()' always returns true
whereas `next()' returns undef all the time. Useful as an extension
basis for infinite sequences.
merge($tream1 [, $tream2 [, $tream3 [, ...]]])
This function merges one or more streams together by fan-outing `next()'
method call among the non-empty streams. Returns a new enumerable
instance, which: * Has next elements as far as at least one of the
streams does. * Returns next element py picking it one-by-one from the
streams. * Is finite if and only if all the streams are finite. If one
of the streams is over, it would be taken into account and `next()' will
continue choosing from non-empty ones.
chain($tream1(, $tream2(, $tream3(, ...))))
Executes streams sequentually, one after another: the next stream starts
once the previous is over.
from_text_file($file_handle(, $options))
Method takes an open file handle and an optional hash of options and
creates a stream of it. The file would be read as a text file, line by
line. For additional options see `open()' perl core function reference.
Options is a basic hash, supported attributes are: * chomp :: Bool |
Whether the lines should be chomped, 0 by default. * is_finite :: Bool |
Forces the stream to be processed as finite, 0 by default.
from_bin_file($file_handle(, $options))
( run in 1.843 second using v1.01-cache-2.11-cpan-39bf76dae61 )