Skip to content

Instantly share code, notes, and snippets.

@frankdejonge
Last active December 28, 2015 16:28
Show Gist options
  • Save frankdejonge/7528801 to your computer and use it in GitHub Desktop.
Save frankdejonge/7528801 to your computer and use it in GitHub Desktop.
PHP Functional Currying
<?php
function __curry($callback, Reflector $reflector = null, $arguments = array(), $numArgs = null)
{
if ( ! $reflector) {
$reflector = new ReflectionFunction($callback);
}
if ($numArgs === null) {
$numArgs = $reflector->getNumberOfRequiredParameters();
}
return function () use ($callback, $reflector, $arguments, $numArgs) {
$arguments = array_merge($arguments, func_get_args());
if (count($arguments) >= $numArgs) {
return $reflector->invokeArgs($arguments);
}
return __curry($callback, $reflector, $arguments, $numArgs);
};
}
$strpos = __curry('strpos');
$strposOfCurry = $strpos('curry');
var_dump($strposOfCurry('y')); // 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment