return $fn($this->a, $this->next);
}
}
-
+const sum = 'example\sum';
function sum(MonadFree $a, MonadFree $b): MonadFree
{
}, $a, $b);
}
+const int = 'example\int';
+
function int(int $int): MonadFree
{
return liftF(new IntVal($int, Pure::of));
}
+const mul = 'example\mul';
+
function mul(MonadFree $a, MonadFree $b): MonadFree
{
return liftM2(function ($a, $b) {
}, $a, $b);
}
+const square = 'example\square';
+
function square(MonadFree $a): MonadFree
{
return $a->bind(function ($a) {
use function Widmogrod\Monad\Maybe\just;
use function Widmogrod\Monad\Maybe\nothing;
+// Some dependencies are needed
+require_once __DIR__ . '/FreeCalculatorTest.php';
+
/**
* ParserF a next
* = RuleChar a (a -> next)
{
return function ($result) use ($fn) {
[$matched, $rest] = $result;
+
return just([
$fn($matched),
$rest
return $b instanceof Just
? $b->bind(function ($result) use ($matcher) {
[$matched, $rest] = $result;
+
return $matcher($rest)->map(function ($result) use ($matched) {
[$matched2, $rest2] = $result;
+
return [concatM($matched, fromValue($matched2)), $rest2];
});
})
function denest(callable $matcher)
{
$map = [];
+
return function (Listt $a) use ($matcher, &$map) {
$key = spl_object_id($a);
if (isset($map[$key])) {
}
$map[$key] = true;
+
return $matcher($a);
};
}
-function tokens(string $input) : Listt {
+function tokens(string $input) : Listt
+{
$tokens = preg_split('//', $input);
$tokens = array_filter($tokens);
$tokens = fromIterable($tokens);
return $tokens;
}
-class FreeParserTest extends \PHPUnit\Framework\TestCase
+class ParserTest extends \PHPUnit\Framework\TestCase
{
/**
* Grammar
&$expression, $operator, &$expression
]), function (Listt $attr) {
[$a, $op, $b] = $attr->extract();
+
return $op($a, $b);
}));