force call if no args supplied for curried function

This commit is contained in:
Kacper Donat 2018-07-15 23:00:49 +02:00
parent f8eb8a0cf7
commit 17c96b2834
2 changed files with 13 additions and 0 deletions

View File

@ -14,6 +14,8 @@ use function Kadet\Functional\reflect;
class CurriedFunction extends PartiallyAppliedFunction class CurriedFunction extends PartiallyAppliedFunction
{ {
const INF = PHP_INT_MAX;
private $threshold; private $threshold;
/** /**
@ -61,6 +63,10 @@ class CurriedFunction extends PartiallyAppliedFunction
*/ */
public function __invoke(...$args) public function __invoke(...$args)
{ {
if (count($args) === 0) {
return $this->call();
}
$applied = $this->apply(...$args); $applied = $this->apply(...$args);
if (count($applied->getArguments()) >= $this->threshold) { if (count($applied->getArguments()) >= $this->threshold) {

View File

@ -111,6 +111,13 @@ class CurriedFunctionTest extends \PHPUnit\Framework\TestCase
$this->assertSame('abc', $g->call('c')); $this->assertSame('abc', $g->call('c'));
} }
public function testForceCallIfNoArgs()
{
$f = f\curry($this->variadic, f\Utils\CurriedFunction::INF);
$this->assertSame('abcdef', $f('a', 'b', 'c')('d', 'e')('f')());
}
public function testUncurry() public function testUncurry()
{ {
$f = f\curry($this->variadic, 10); $f = f\curry($this->variadic, 10);