Last active
December 28, 2015 16:28
-
-
Save frankdejonge/7528801 to your computer and use it in GitHub Desktop.
PHP Functional 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 __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