NAME
FAST::List::Gen::Lazy - perl6 / haskell like laziness in perl5
SYNOPSIS
this module provides tools to implement perl6/haskell style lazy programming in perl5.
this module is a mixin to FAST::List::Gen that adds functions to FAST::List::Gen's namespace and exportable function list
FUNCTIONS
- lazypipe
LIST -
lazypipeprovides a lazy list implementation that will expand generators.two methods are provided,
->nextwhich returns the next item from the pipe, and->morewhich returns true if there are more items in the pipe. the pipe works with aliases to its argument list, and never touches or copies any items until it has to.lazypipeprovides the behavior of thelazygenerator. - lazyflatten
LIST -
lazyflattenis just likelazypipeexcept it will also expand array references and subroutines.lazyflattenprovides the behavior of thelazyxgenerator. - lazy
LIST - L
LIST -
lazyis alazypipewrapped inside of an iterative generator. ifLISTis one item, and is already a generator, that generator is returned unchanged. - lazyx
LIST - Lx
LIST -
lazyxis alazyflattenwrapped inside of an iterative generator. ifLISTis one item, and is already a generator, that generator is returned unchanged. - fn
CODE [ARITY] [RETURNS] -
fnconverts a subroutine into a subroutine with partial application and lazy evaluation.my $add3 = fn {$_[0] + $_[1] + $_[2]} 3; my $add2 = $add3->(my $first); my $add1 = $add2->(my $second); my $sum1 = $add1->(4); my $sum2 = $add1->(8); $first = 10; $second = 100; say $sum1; # prints 114 $second = 800; say $sum1; # still prints 114 say $sum2; # prints 818fnsupports subroutine prototypes, and can determineARITYfrom them.ARITYdefaults to 1, with a prototype of(@).ARITYcan be given as a prototype string'&@'or an integer.the
RETURNSdefaults to 1, and specifies the number of values that will be returned by the function (the number of thunk accessors to create). for example, thesplitAtfunction in FAST::List::Gen::Haskell is implemented as:*splitAt = fn {take(@_), drop(@_)} 2, 2; my ($xs, $ys) = splitAt(3, <1..>); # 2 thunk accessors are created but # take() and drop() have not been called say $xs->str; # 1 2 3 say $ys->str(5); # 4 5 6 7 8due to partial application, you can even call subs in a way that looks a bit like the haskell type signature, should you so desire.
my ($xs, $ys) = splitAt -> (3) -> (<1..>);most of the functions in FAST::List::Gen::Haskell are implemented with
fn - now
LIST -
sometimes the return values of
fn {...}are too lazy.nowwill force the values inLISTto evaluate, and will return the new list.now(...) ~~ grep {!$_ or 1} ... - methods of
fn {...}functions -
return values of
fn {...}have the following overloaded behaviors and methods$fn . $code $fn->compose($code) sub {$fn->(&$code)} $fn << $val $fn->curry($val) sub {$fn->($val, @_)} $fn >> $val $fn->rcurry($val) sub {$fn->(@_, $val)} ~$fn $fn->flip sub {$fn->(@_[reverse 0 .. $#_])}some more complex examples, assuming the functions from FAST::List::Gen::Haskell
my $second = \&head . \&tail; my $third = \&head . \&tail . \&tail; my $join = \&foldl << sub {$_[0] . $_[1]}; my $ucjoin = sub {uc $_[0]} . $join; my $cycle = \&cycle << '[' >> ']'; my $joined_cycle = $ucjoin . take(18) . $cycle; say $joined_cycle->(qw(1 a 2 b)); # '[1A2B][1A2B][1A2B]'the overloaded operators for functions do not seem to work properly in perl's before 5.10. the corresponding methods can be used instead.
AUTHOR
Eric Strom, <asg at cpan.org>
BUGS
report any bugs / feature requests to bug-list-gen at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=List-Gen.
comments / feedback / patches are also welcome.
COPYRIGHT & LICENSE
copyright 2009-2011 Eric Strom.
this program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
see http://dev.perl.org/licenses/ for more information.