metaperl has asked for the wisdom of the Perl Monks concerning the following question:

I have a subroutine which returns a word based on the results of applying a closure to a test value:
sub morf { my($tester, $testval, $true_word, $false_word)=@_; $tester->($testval) ? $true_word : $false_word ; }
Now, I want to curry string equal so that my function morf returns 'Male' if the $testval is 'M' and otherwise female:
sub se { my ($val) = @_; sub { $_[0] eq $val } ; }
morf(se('M'), $driver->{gender}, 'Male', 'Female') ;
How can this be done?

Replies are listed 'Best First'.
Re: currying CORE functions into closures?
by roboticus (Chancellor) on May 01, 2009 at 22:35 UTC
    metaperl:

    Doesn't it just work?

    roboticus@swill $ cat 761397.pl #!/usr/bin/perl -w use strict; use warnings; sub morf { my ($tester, $testval, $true_word, $false_word) = @_; $tester->($testval) ? $true_word : $false_word; } sub se { my ($val) = @_; sub { $_[0] eq $val} } my $fr = se('M'); for my $FL (split /|/, "MNLOP") { print "FL=$FL, MORF=", morf($fr, $FL, 'Male', 'Female'), "\n"; } roboticus@swill $ ./761397.pl FL=M, MORF=Male FL=N, MORF=Female FL=L, MORF=Female FL=O, MORF=Female FL=P, MORF=Female Male Female Male
    ...roboticus
Re: currying CORE functions into closures?
by revdiablo (Prior) on May 01, 2009 at 21:13 UTC

    Maybe I don't understand your question, but aren't you already doing that with se?