Skip to content

Commit c875bf9

Browse files
committed
Add Phoenix Combinator.
1 parent b8d0633 commit c875bf9

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace spec\loophp\combinator\Combinator;
6+
7+
use PhpSpec\ObjectBehavior;
8+
9+
class PhoenixSpec extends ObjectBehavior
10+
{
11+
public function it_is_initializable()
12+
{
13+
$f = static function ($a) {
14+
return static function ($b) use ($a) {
15+
return 'f(' . $a . ')(' . $b . ')';
16+
};
17+
};
18+
19+
$g = static function ($a) {
20+
return 'g(' . $a . ')';
21+
};
22+
23+
$h = static function ($a) {
24+
return 'h(' . $a . ')';
25+
};
26+
27+
$arguments = [$f, $g, $h, 'x'];
28+
29+
$this->beConstructedWith(...$arguments);
30+
31+
$this()
32+
->shouldReturn('f(g(x))(h(x))');
33+
34+
$this
35+
->__invoke()
36+
->shouldReturn('f(g(x))(h(x))');
37+
}
38+
}

src/Combinator/Phoenix.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace loophp\combinator\Combinator;
6+
7+
use loophp\combinator\Combinator;
8+
9+
/**
10+
* Class Phoenix.
11+
*
12+
* @psalm-template AType
13+
* @psalm-template BType
14+
* @psalm-template CType
15+
* @psalm-template DType
16+
*/
17+
final class Phoenix extends Combinator
18+
{
19+
/**
20+
* @var callable
21+
*/
22+
private $f;
23+
24+
/**
25+
* @var callable
26+
*/
27+
private $g;
28+
29+
/**
30+
* @var callable
31+
*/
32+
private $h;
33+
34+
/**
35+
* @var mixed
36+
*/
37+
private $x;
38+
39+
/**
40+
* Psi constructor.
41+
*
42+
* @psalm-param callable(BType) : callable(CType) : DType $f
43+
* @psalm-param callable(AType) : BType $g
44+
* @psalm-param callable(AType) : CType $h
45+
* @psalm-param AType $x
46+
*
47+
* @param callable $f
48+
* @param callable $g
49+
* @param callable $h
50+
* @param mixed $x
51+
*/
52+
public function __construct(callable $f, callable $g, callable $h, $x)
53+
{
54+
$this->f = $f;
55+
$this->g = $g;
56+
$this->h = $h;
57+
$this->x = $x;
58+
}
59+
60+
/**
61+
* @psalm-return DType
62+
*/
63+
public function __invoke()
64+
{
65+
return ($this->f)(($this->g)($this->x))(($this->h)($this->x));
66+
}
67+
}

0 commit comments

Comments
 (0)