51 lines
1.2 KiB
PHP
51 lines
1.2 KiB
PHP
<?php
|
|
/**
|
|
* Copyright 2018 Kacper Donat
|
|
*
|
|
* @author Kacper "Kadet" Donat <kacper@kadet.net>
|
|
*
|
|
* Full license available in separate LICENSE file
|
|
*/
|
|
|
|
namespace Kadet\Functional;
|
|
|
|
use Kadet\Functional\Utils\CurriedFunction;
|
|
use Kadet\Functional\Utils\FixedFunction;
|
|
use Kadet\Functional\Utils\FunctionPipeline;
|
|
use Kadet\Functional\Utils\PartiallyAppliedFunction;
|
|
|
|
function partial(callable $callable, ...$args): PartiallyAppliedFunction
|
|
{
|
|
return new PartiallyAppliedFunction($callable, ...$args);
|
|
}
|
|
|
|
function apply(callable $callable, ...$args)
|
|
{
|
|
$threshold = reflect($callable)->getNumberOfRequiredParameters();
|
|
|
|
return count($args) >= $threshold
|
|
? $callable(...$args)
|
|
: partial($callable, ...$args);
|
|
}
|
|
|
|
function curry(callable $callable, int $threshold = null, ...$args): CurriedFunction
|
|
{
|
|
return new CurriedFunction($callable, $threshold, ...$args);
|
|
}
|
|
|
|
function uncurry(CurriedFunction $curried): PartiallyAppliedFunction
|
|
{
|
|
return $curried->uncurry();
|
|
}
|
|
|
|
function pipe(...$functions)
|
|
{
|
|
return new FunctionPipeline(...$functions);
|
|
}
|
|
|
|
function fix(callable $callable, ...$args)
|
|
{
|
|
$f = new FixedFunction($callable);
|
|
return $args ? apply($f, ...$args) : $f;
|
|
}
|