Acme-Lambda
view release on metacpan or search on metacpan
lib/Acme/Lambda.pm view on Meta::CPAN
package Acme::Lambda;
use 5.008;
use warnings;
use strict;
use utf8;
use base qw(Exporter);
our @EXPORT = qw(lambda λ);
our @EXPORT_OK = @EXPORT;
sub lambda(&) {
my $sub = shift;
return sub {local $_ = $_[0]; $sub->(@_)};
}
*λ = \λ
=encoding utf8
=head1 NAME
Acme::Lambda - Perl with lambdas!
=head1 VERSION
Version 0.03
=cut
lib/Acme/Lambda.pm view on Meta::CPAN
=head1 SYNOPSIS
Acme::Lambda brings the power of lambda to perl! It exports lambda and
λ subroutines that take a code block and return it.
use Acme::Lambda;
my $square = lambda { $_ * $_ };
print $square->(4); # 16
use utf8;
my $cube = λ {$_ * $_ * $_};
print $cube->(3); # 27
# The sub can also access its full argument list through @_
my $add = lambda {$_[0] + $_[1] } ;
print $add->(3,4); # 7
=head1 EXPORT
t/01-basic.t view on Meta::CPAN
use Test::More qw(no_plan);
use Acme::Lambda;
use utf8;
my $sub = lambda {my $x = shift; $x*$x};
is(ref($sub), "CODE", "lambda returns a subref");
is($sub->(4), 16, "subref returns correct value");
my $sub2 = λ{my $x = shift; $x*$x};
is(ref($sub2), "CODE", "λ returns a subref");
is($sub2->(4), 16, "subref returns correct value");
t/02-onearg.t view on Meta::CPAN
Test the use of $_ to access the lambda's first argument
=cut
use Acme::Lambda;
my $square = lambda { $_ * $_ };
is($square->(4), 16);
use utf8;
my $cube = λ {$_ * $_ * $_};
is($cube->(3), 27);
# Make sure lambda doesn't clobber $_
$_ = "something";
lambda{$_}->('else');
is($_, "something");
( run in 0.349 second using v1.01-cache-2.11-cpan-4d50c553e7e )