NAME
HOI::Comprehensions - Higher-Order Imperative "Re"features in Perl: List Comprehensions
SYNOPSIS
use HOI::Comprehensions qw( comp );
my $list = comp( sub { $x + $y + $z }, x => [ 1, 2, 3 ], y => [ 4, 5, 6 ], z => sub { ( 1, 1 ) } )->( sub { $x > 1 } );
my ($elt, $done);
do {
($elt, $done) = <$list>;
print "$elt ";
} while (not $done);
print "\n";
DESCRIPTION
HOI::Comprehensions offers lazy-evaluated list comprehensions with limited support to generators of an infinite list. It works as if evaluating multi-level loops lazily.
Currently, the generators are handled in sequence of the argument list offered by the user. As a result of such implementation, a list { (x, y) | x is natural number, y belongs to { 0, 1 }, x is odd } may be evaluated incorrectly. To avoid such situation, make sure finite generators are subsequent to all the infinite ones.
FUNCTIONS
comp($@)->(@)
For creating a list comprehension object. The formula for computing the elements of the list is given as a subroutine, following by the generators, in form of name => arrayref or name => subroutine. Comp returns a function which takes all guards in form of subroutines.
A hashref which holds generator variables as its keys and value of those variables as its values is passed to the formula subroutine. However, it is recommended to use such variables directly instead of dereference the hashref.
Generators can be arrayrefs, subroutines or list comprehensions. A subroutine generator should return a pair ( elt, done ), where elt is the next element and done is a flag telling whether the iteration is over.
METHODS
get_list
Get the list member of a list comprehension object. It returns a arrayref which holds the actual list evaluated so far.
is_over
Returns a boolean which tells if the evaluation of the list is over.
get_member($)
Get a member of a list comprehension object by name. A list comprehension object is actually a blessed hashref.
OPERATORS
<>
List evaluation iterator. Returns the "next" element generated in the sequence in the situation of eager evaluation, and a flag telling whether the evaluation is done.
+
List indexing operator. Takes an integer as the index and a list comprehension object. The operator returns an arrayref [ $elt, $done ], where $elt is the element at the given index and $done is a flag telling whether the evaluation is done.
AUTHOR
withering <withering@cpan.org>
COPYRIGHT AND LICENSE
Copyright (C) 2014 by withering
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.20.0 or, at your option, any later version of Perl 5 you may have available.