Example #1
0
File: fp.php Project: slifin/php-fp
/**
 * Internal function used for the main currying functionality
 * @param callable $fn
 * @param $appliedArgs
 * @param $requiredParameters
 * @return callable
 */
function _curry(callable $fn, $appliedArgs, $requiredParameters)
{
    return function (...$args) use($fn, $appliedArgs, $requiredParameters) {
        $originalArgs = $appliedArgs;
        $newArgs = $args;
        array_push($appliedArgs, ...$args);
        // Get the number of arguments currently applied
        $appliedArgsCount = count(array_filter($appliedArgs, function ($v) {
            if ($v instanceof Placeholder) {
                return false;
            }
            return true;
        }));
        // If we have the required number of arguments call the function
        if ($appliedArgsCount >= $requiredParameters) {
            foreach ($appliedArgs as $k => $v) {
                if ($v instanceof Placeholder) {
                    $appliedArgs[$k] = array_shift($newArgs);
                    unset($appliedArgs[count($originalArgs) + $k]);
                }
            }
            return $fn(...$appliedArgs);
            // If we will have the required arguments on the next call, return an optimized function
        } elseif ($appliedArgsCount + 1 === $requiredParameters) {
            return bind($fn, ...$appliedArgs);
            // Return the standard full curry
        } else {
            return _curry($fn, $appliedArgs, $requiredParameters);
        }
    };
}
Example #2
0
/**
 * Internal function used for the main currying functionality
 * @param callable $fn
 * @param $appliedArgs
 * @param $requiredParameters
 * @return callable
 */
function _curry(callable $fn, $appliedArgs, $requiredParameters)
{
    return function (...$args) use($fn, $appliedArgs, $requiredParameters) {
        array_push($appliedArgs, ...$args);
        // Get the number of arguments currently applied
        $appliedArgsCount = count($appliedArgs);
        // If we have the required number of arguments call the function
        if ($appliedArgsCount >= $requiredParameters) {
            return $fn(...$appliedArgs);
            // If we will have the required arguments on the next call, return an optimized function
        } elseif ($appliedArgsCount + 1 === $requiredParameters) {
            return bind($fn, ...$appliedArgs);
            // Return the standard full curry
        } else {
            return _curry($fn, $appliedArgs, $requiredParameters);
        }
    };
}