I've even been playing with the more extreme idea that the even if you have other language features associated with the functional style, perhaps it is a good idea to limit yourself to partial application. The language features I'm thinking of avoiding are anonymous, nestable, and lexically-scoped functions.
I'll leave it to another post to explain these ideas. All I really want to do here is share my implementations of partial application in Perl and PHP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub Pa # Partially Apply | |
{ | |
my ( $f, @a ) = @_; | |
sub { $f->( @a, @_ ) } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function Pa() // Partially Apply | |
{ | |
$origArgs = func_get_args(); | |
return function() use ( $origArgs ) | |
{ | |
$allArgs = array_merge( $origArgs, func_get_args() ); | |
return call_user_func_array( 'call_user_func', $allArgs ); | |
}; | |
} | |
?> |
No comments:
Post a Comment