-
-
Save andreiglingeanu/8f04495f6992a28bf6d7caa4567e1f7f to your computer and use it in GitHub Desktop.
Partial function application with PHP
This file contains hidden or 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 foo($a, $b) { | |
echo "a: {$a} b: {$b}\n"; | |
} | |
$foo_caller = new PartialCallable('foo', array('A')); | |
$foo_caller('B'); | |
$foo_caller = partial_function('foo', 'A'); | |
$foo_caller('B'); |
This file contains hidden or 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 | |
class PartialCallable { | |
function __construct($callable, $args) { | |
$this->callable = $callable; | |
$this->args = $args; | |
} | |
function __invoke() { | |
call_user_func_array($this->callable, array_merge($this->args, func_get_args())); | |
} | |
} |
This file contains hidden or 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 partial_function() { | |
$applied_args = func_get_args(); | |
return function() use($applied_args) { | |
return call_user_func_array('call_user_func', array_merge($applied_args, func_get_args())); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment