Perl6::Currying

Perl6::Currying is a Perl 6 subroutine currying for Perl 5.
Download

Perl6::Currying Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Damian Conway
  • Publisher web site:
  • http://search.cpan.org/~dconway/

Perl6::Currying Tags


Perl6::Currying Description

Perl6::Currying is a Perl 6 subroutine currying for Perl 5. Perl6::Currying is a Perl 6 subroutine currying for Perl 5.SYNOPSIS use Perl6::Currying; sub add ($a,$b) { $a + $b } # Define a sub with named params print add(1,2); # Call it my $incr = &add.prebind(a=>1); # Bind the $a argument to 1 # to create an increment subroutine print $incr->(3), "n"; # Increment a numberThe 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: # Perl 6 code 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: # Perl 6 code 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: # Perl 6 code my $pi_approx = ÷.prebind(numerator=>22,denominator=>7); print $pi_approx(); # prints 3.14285714285714or in stages: # Perl 6 code my $pi_legislated = $halve.prebind(numerator=>6); print $pi_legislated(); # prints 3Note 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: # Perl 6 code &reciprocal := ÷.prebind(numerator=>1); print reciprocal(10) # prints 0.1Parameter binding in Perl 5The Perl6::Currying module allows you to use the same syntax in Perl 5.That is, you can supply some of the arguments to a (specially prototyped) Perl 5 subroutine and thereby create another subroutine that calls the original with those arguments automatically supplied.The new subroutine is created by calling the prebind method on the original subroutine. For example: # Perl 5 code use Perl6::Currying; sub divide ($numerator, $denominator) { return $numerator / $denominator; } my $halve = ÷.prebind(denominator=>2);Notes:As the above example implies, Perl6::Currying gives you the (limited) ability to declare Perl 5 subroutines with named parameters. Currently those parameters must be a list of comma-separated scalars, as shown above. Each parameter becomes a lexical scalar variable within the body of the subroutine.For forward compatibility, to prebind parameters in Perl 5, the Perl 6 method call syntax ($objref.methodname(...)) is used, rather than the Perl 5 syntax ($objref->methodname(...)).To be consistent with Perl 6, it's still necessary to use the & sigil to indicate that the method to be called is CODE::prebind, not the prebind of the object returned by calling divide.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: # Perl 5 code print divide(42,2); # calls ÷...prints 21 print $halve->(42); # calls ÷...prints 21Note that since these are just normal Perl 5 subroutine calls, the Perl 5 call-through-reference syntax ($subref->(...)) is used, rather than the Perl 6 syntax ($subref.(...)).It's also possible to prebind all the arguments of a subroutine, either all at once: # Perl 5 code use Perl6::Currying; my $pi_approx = ÷.prebind(numerator=>22,denominator=>7); print $pi_approx->(); # prints 3.14285714285714or in stages: # Perl 5 code use Perl6::Currying; my $pi_legislated = $halve.prebind(numerator=>6); print $pi_legislated(); # prints 3You can also use Perl 5 typeglobs to create new named subroutines by partially binding existing ones. For example: # Perl 5 code *reciprocal = ÷.prebind(numerator=>1); print reciprocal(10) # prints 0.1Requirements:· Perl


Perl6::Currying Related Software