PrePAN

Sign in to PrePAN

Profile

User's Modules

Keyword::TailRecurse A small module that added a tailRecurse keyword

Keyword::TailRecurse provides a tailRecurse keyword that does proper tail recursion that doesn't grow the call stack. It uses functionality added in Perl 5.14 so it's a Pure Perl solution (other current solutions use XS modules).

After using the module you can precede a function call with the keyword tailRecurse and rather adding a new entry on the call stack the function call will replace the current entry on the call stack.

note: Keyword::TailRecurse requires features Perl v5.14 and above.

SEE ALSO

There's a couple of existing modules that use XS modules to provide similar functionality:

  • Sub::Call::Recur

    An XS module that provides a form of tail recursion - limited to recursing into the same function it's used from.

  • Sub::Call::Tail

    An XS module that provides a generic tail recursion.

jlcooper@github 1 comment

PartialApplication A small module that handle partially applying parameters to functions

PartialApplilcation is a small module to handle partially applying parameters to functions - a common technique used in the functional programming style of coding.

FUNCTIONS

partiallyApply( \&function, @parameters )

Partially applies the parameters to the function, giving a new function reference.

To partially apply parameters to an object method, pass a function reference to the method as the first parameter, the object instance as the second, followed by the parameters to be partially applied.

my $object = Class->new();

my $partiallyAppliedMethod = partiallyApply( \&Class::method, $object, 1, 2, 3 );

$partiallyAppliedMethod->(4, 5, 6);   # equivilant of $object->method(1, 2, 3, 4, 5, 6)

RETURNS

A function reference to the partially applied function.

partiallyApplyRight( \&function, @parameters )

Partially applies the parameters to the end of the function call - useful for when you're using named parameters and you want to make sure that the partially applied parameters are the ones used.

sub testSub {
    my %params = @_;

    print "$_ - $params{$_}\n" for keys %params;
}

my $partiallyAppliedFunction = partiallyApplyRight( \&testSub, asdf => 99 );

$partiallyAppliedFunction->(asdf => 100);   # outputs: asdf - 99

RETURNS

A function reference to the partially applied function.

partiallyApplyN( \&function, \@parameterBitmap, @parameters )

Partially applies the parameters based upon the parameterBitmap. An entry of 1 in the parameterBitmap will use a partially applied parameter and an entry of 0 will use a parameter from the call.

sub testSub {
    print join(", ", @_) ."\n";
}

my $partiallyAppliedFunction = partiallyApplyN( \&testSub, [ 1, 0, 1, 0 ], 1, 2, 3, 4, 5 );

$partiallyAppliedFunction->('a', 'b', 'c', 'd'); # outputs: 1, a, 2, b, 3, 4, 5, c, d

This does allow you to partially apply parameter to a method call without specifying the object instance to apply it to multiple instances.

my $partiallyAppliedMethod2 = partiallyApplyN( \&Class::method, [ 0, 1, 1, 1 ], 1, 2, 3 );

$partiallyAppliedMethod2->($object1, 4, 5); # equivilant to $object1->method(1, 2, 3, 4, 5)
$partiallyAppliedMethod2->($object2, 6, 7); # equivilant to $object2->method(1, 2, 3, 6, 7)

RETURNS

A function reference to the partially applied function.

jlcooper@github 2 comments