Created
December 6, 2010 12:10
-
-
Save CHH/730201 to your computer and use it in GitHub Desktop.
Prefill the arguments of functions (Currying)
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 func_curry($fn) | |
{ | |
$args = array_slice(func_get_args(), 1); | |
return function() use ($fn, $args) { | |
$args = array_merge($args, func_get_args()); | |
return call_user_func_array($fn, $args); | |
}; | |
} | |
$a = function() { | |
return join(func_get_args(), ", "); | |
}; | |
$b = func_curry($a, "A", "B", "C"); | |
echo $b("D", "E"); // -> A, B, C, D, E | |
// works on built-in functions too | |
$b = func_curry("strtoupper", "foo bar baz"); | |
echo $b(); // -> FOO BAR BAZ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment