Skip to content

Instantly share code, notes, and snippets.

@jiripudil
Created January 9, 2015 10:53
Show Gist options
  • Save jiripudil/0a6450da13cc7747c5ce to your computer and use it in GitHub Desktop.
Save jiripudil/0a6450da13cc7747c5ce to your computer and use it in GitHub Desktop.
Helper function for partial application in PHP
<?php
function partial(callable $closure, ...$partialArgs) {
return function (...$args) use ($closure, $partialArgs) {
return call_user_func($closure, ...$partialArgs, ...$args);
};
}
// demo
$foo = function ($a, $b, $c) {
var_dump(func_get_args());
};
$bar = partial($foo, "A");
$baz = partial($bar, 42);
call_user_func($baz, "C");
/*
array(3) {
[0] =>
string(1) "A"
[1] =>
int(42)
[2] =>
string(1) "C"
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment