Skip to content

Commit be79b0a

Browse files
committed
Add PartialCallabte::partial()
1 parent cf9b3bd commit be79b0a

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/Functools/PartialCallable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ public function __invoke()
3131
return call_user_func_array($this->callable, $this->arguments(func_get_args()));
3232
}
3333

34+
/**
35+
* Make partially applied function
36+
*
37+
* @note Returns new instance
38+
*/
39+
public function partial(array $arguments)
40+
{
41+
return new PartialCallable($this->callable, $this->arguments + $arguments, $this->pos);
42+
}
43+
3444
private function arguments(array $additional_arguments)
3545
{
3646
$arguments = $this->arguments;

tests/Functools/PartialCallableTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33

44
final class PartialCallableTest extends \PHPUnit_Framework_TestCase
55
{
6+
public function test_partial()
7+
{
8+
$func = function ($a, $b, $c) { return $a.$b.$c; };
9+
$p1 = new PartialCallable($func, [2 => "c"], 1);
10+
11+
$p2 = $p1->partial([0 => "A"]);
12+
$this->assertInstanceOf('Teto\Functools\PartialCallable', $p2);
13+
14+
$this->assertEquals("ABc", $p2("B"));
15+
}
16+
617
/**
718
* @dataProvider dataProviderFor_array_map
819
*/

0 commit comments

Comments
 (0)