Skip to content

Instantly share code, notes, and snippets.

@CHH
Created December 6, 2010 12:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CHH/730201 to your computer and use it in GitHub Desktop.
Save CHH/730201 to your computer and use it in GitHub Desktop.
Prefill the arguments of functions (Currying)
<?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