Name : perl-Perl6-Currying
| |
Version : 0.05
| Vendor : obs://build_opensuse_org/devel:languages:perl
|
Release : 8.69
| Date : 2024-08-05 17:36:31
|
Group : Development/Libraries/Perl
| Source RPM : perl-Perl6-Currying-0.05-8.69.src.rpm
|
Size : 0.02 MB
| |
Packager : (none)
| |
Summary : Perl 6 subroutine currying for Perl 5
|
Description :
The Perl6::Currying module lets you try out the new Perl 6 explicit higher-order function syntax in Perl 5.
In Perl 6 any subroutine can be \"partially bound\". That is, you can supply some of its arguments and thereby create another subroutine that calls the original with those arguments automatically supplied.
Subroutine parameters are partially bound by calling the \'prebind\' method on the subroutine. This method call returns a reference to a new subroutine that calls the original subroutine, inserting into its argument list the prebound arguments. For example:
sub divide ($numerator, $denominator) { return $numerator / $denominator; }
my $halve = ÷.prebind(denominator=>2);
Note that it\'s necessary to use the \'&\' sigil to indicate that the method \'CODE::prebind\' is to be called on a \'CODE\' object \'÷\', not the \'Whatever::prebind\' of the \'Whatever\' object returned by _calling_ \'divide\'. To get the latter, we would write:
divide().prebind(...)
or:
divide.prebind(...)
Having prebound the denominator, if we now call the subroutine referred to by \'$halve\' the effect is to call \'divide\' with an automagically supplied denominator of 2. That is:
print divide(42,2); # calls ÷...prints 21 print $halve(42); # calls ÷...prints 21
It\'s also possible to prebind _all_ the arguments of a subroutine, either all at once:
my $pi_approx = ÷.prebind(numerator=>22,denominator=>7);
print $pi_approx(); # prints 3.14285714285714
or in stages:
my $pi_legislated = $halve.prebind(numerator=>6);
print $pi_legislated(); # prints 3
Note that we _didn\'t_ need the \'&\' sigil before \'$halve\' since this syntax is unambiguously a call (through a reference to a \'CODE\' object) to \'CODE::prebind\'.
You can also use the Perl 6 aliasing operator (\':=\') to create new named subroutines by partially binding existing ones. For example:
&reciprocal := ÷.prebind(numerator=>1);
print reciprocal(10) # prints 0.1
|
RPM found in directory: /packages/linux-pbone/ftp5.gwdg.de/pub/opensuse/repositories/devel:/languages:/perl:/CPAN-P/openSUSE_Tumbleweed/noarch |