From 17c96b2834820a7fa96345b20fadfbffb42a4d0a Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Sun, 15 Jul 2018 23:00:49 +0200 Subject: [PATCH] force call if no args supplied for curried function --- src/Utils/CurriedFunction.php | 6 ++++++ tests/CurriedFunctionTest.php | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/src/Utils/CurriedFunction.php b/src/Utils/CurriedFunction.php index 5a13381..db0b9ae 100644 --- a/src/Utils/CurriedFunction.php +++ b/src/Utils/CurriedFunction.php @@ -14,6 +14,8 @@ use function Kadet\Functional\reflect; class CurriedFunction extends PartiallyAppliedFunction { + const INF = PHP_INT_MAX; + private $threshold; /** @@ -61,6 +63,10 @@ class CurriedFunction extends PartiallyAppliedFunction */ public function __invoke(...$args) { + if (count($args) === 0) { + return $this->call(); + } + $applied = $this->apply(...$args); if (count($applied->getArguments()) >= $this->threshold) { diff --git a/tests/CurriedFunctionTest.php b/tests/CurriedFunctionTest.php index 05ee285..e289c78 100644 --- a/tests/CurriedFunctionTest.php +++ b/tests/CurriedFunctionTest.php @@ -111,6 +111,13 @@ class CurriedFunctionTest extends \PHPUnit\Framework\TestCase $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() { $f = f\curry($this->variadic, 10);