Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
bootstrap = "vendor/autoload.php"
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
Expand Down
23 changes: 22 additions & 1 deletion src/Cypress/Curry/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
function curry($callable)
{
if (_number_of_required_params($callable) < 2)
return _make_function($callable);
return _curry_array_args($callable, _rest(func_get_args()));
}

Expand All @@ -28,6 +30,8 @@ function curry_args($callable, array $args)
*/
function curry_right($callable)
{
if (_number_of_required_params($callable) < 2)
return _make_function($callable);
return _curry_array_args($callable, _rest(func_get_args()), false);
}

Expand Down Expand Up @@ -92,7 +96,7 @@ function _rest(array $args)
*/
function _is_fullfilled($callable, $args)
{
return count($args) === _number_of_required_params($callable);
return count($args) >= _number_of_required_params($callable);
}

/**
Expand All @@ -110,3 +114,20 @@ function _number_of_required_params($callable)
$refl = new \ReflectionFunction($callable);
return $refl->getNumberOfRequiredParameters();
}

/**
* if the callback is an array(instance, method),
* it returns an equivalent function for PHP 5.3 compatibility.
*
* @internal
* @param callable $callable
* @return callable
*/
function _make_function($callable)
{
if (is_array($callable))
return function() use($callable) {
return call_user_func_array($callable, func_get_args());
};
return $callable;
}
54 changes: 54 additions & 0 deletions tests/Cypress/Curry/functions_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,60 @@ public function test_curry_right_args_three_times()
$this->assertEquals(2, $divideBy10And5(100));
}

public function test_curry_using_func_get_args()
{

$fnNoArgs = function () { return func_get_args(); };
$curried = C\curry($fnNoArgs);
$curriedRight = C\curry_right($fnNoArgs);

$this->assertEquals(array(), $fnNoArgs());
$this->assertEquals(array(), $curried());
$this->assertEquals(array(), $curriedRight());

$this->assertEquals(array(1), $fnNoArgs(1));
$this->assertEquals(array(1), $curried(1));
$this->assertEquals(array(1), $curriedRight(1));

$this->assertEquals(array(1, 2, 'three'), $fnNoArgs(1, 2, 'three'));
$this->assertEquals(array(1, 2, 'three'), $curried(1, 2, 'three'));
$this->assertEquals(array(1, 2, 'three'), $curriedRight(1, 2, 'three'));

$fnOneArg = function ($x) { return func_get_args(); };
$curried = C\curry($fnOneArg);
$curriedRight = C\curry_right($fnOneArg);

$this->assertEquals(array(1), $fnOneArg(1));
$this->assertEquals(array(1), $curried(1));
$this->assertEquals(array(1), $curriedRight(1));

$this->assertEquals(array(1, 2, 'three'), $fnOneArg(1, 2, 'three'));
$this->assertEquals(array(1, 2, 'three'), $curried(1, 2, 'three'));
$this->assertEquals(array(1, 2, 'three'), $curriedRight(1, 2, 'three'));

$fnTwoArgs = function ($x, $y) { return func_get_args(); };
$curried = C\curry($fnTwoArgs);
$curriedRight = C\curry_right($fnTwoArgs);

$curriedOne = $curried(1);
$curriedRightOne = $curriedRight(2);
$curriedRightTwo = $curriedRight('three');

$this->assertEquals(array(1, 2), $fnTwoArgs(1, 2));
$this->assertEquals(array(1, 2), $curried(1, 2));
$this->assertEquals(array(1, 2), $curriedRight(2, 1));

$this->assertEquals(array(1, 2, 'three'), $fnTwoArgs(1, 2, 'three'));
$this->assertEquals(array(1, 2, 'three'), $curried(1, 2, 'three'));
$this->assertEquals(array(1, 2, 'three'), $curriedRight('three', 2, 1));

$this->assertEquals(array(1, 2), $curriedOne(2));
$this->assertEquals(array(1, 2), $curriedRightOne(1));

$this->assertEquals(array(1, 2, 'three'), $curriedOne(2, 'three'));
$this->assertEquals(array(1, 2, 'three'), $curriedRightTwo(2, 1));
}

public function test_rest()
{
$this->assertEquals(array(1), C\_rest(array(1, 1)));
Expand Down