NAME
List::Gen::Perl6 - perl6 meta operators in perl5
SYNOPSIS
Many of the features found in List::Gen borrow ideas from perl6. However, since the syntax of perl5 and perl6 differ, some of the constructs in perl5 are longer/messier than in perl6. List::Gen::Perl6
is a source filter that makes some of List::Gen
's features more syntactic.
the new syntactic constructs are:
cross: generator X generator
crosswith: generator X+ generator
zip: generator Z generator
zipwith: generator Z+ generator
hyper: generator <<+>> generator
hyper: generator >>+<< generator
hyper: generator >>+>> generator
hyper: generator <<+<< generator
reduce: [+] list
triangular reduction: [\+] list
or [..+] list
in the above, +
can be any perl binary operator.
here is a table showing the correspondence between the source filter constructs, the native overloaded ops, and the operation expanded into methods and functions.
List::Gen::Perl6 List::Gen List::Gen expanded
<1..3> Z <4..6> ~~ <1..3> | <4..6> ~~ <1..3>->zip(<4..6>)
<1..3> Z. <4..6> ~~ <1..3> |'.'| <4..6> ~~ <1..3>->zipwith(sub {$_[0] . $_[1]}, <4..6>)
<1..3> X <4..6> ~~ <1..3> x <4..6> ~~ <1..3>->cross(<4..6>)
<1..3> X. <4..6> ~~ <1..3> x'.'x <4..6> ~~ <1..3>->crosswith(sub {$_[0] . $_[1]}, <4..6>)
[+] 1..10 ~~ <[+] 1..10> ~~ reduce {$_[0] + $_[1]} 1 .. 10
[+]->(1..10) ~~ <[+]>->(1..10) ~~ same as above
[\+] 1..10 ~~ <[..+] 1..10> ~~ scan {$_[0] + $_[1]} 1 .. 10
[\+]->(1..10) ~~ <[..+]>->(1..10) ~~ same as above
<1..3> <<+>> <4..6> ~~ <1..3> <<'+'>> <4..6> ~~ gen {$$_[0] + $$_[1]} tuples <1..3>, <4..6>
Except for normal reductions [+]
, all of the new constructs return a generator.
When used without a following argument, reductions and triangular reductions will return a code reference that will perform the reduction on its arguments.
my $sum = [+];
say $sum->(1..10); # 55
Reductions can take a list of scalars, or a single generator as their argument.
Only the left hand side of the zip, cross, and hyper operators needs to be a generator. zip and cross will upgrade their rhs to a generator if it is an array. hyper will upgrade it's rhs to a generator if it is an array or a scalar.
The source filter is limited in scope, and should not harm other parts of the code, however, source filters are notoriously difficult to fully test, so take that with a grain of salt. Due to limitations of Filter::Simple, hyper operators will be filtered in both code and strings. All other filters should skip strings.
This code is not really intended for serious work, ymmv.
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.