{
$result = doo(
let('a', Identity::of(1)),
- let('b', Identity::of(2)),
+ let('b', Identity::of(3)),
let('c', in(['a', 'b'], function (int $a, int $b): Identity {
return Identity::of($a + $b);
})),
})
);
- $this->assertEquals(Identity::of(9), $result);
+ $this->assertEquals(Identity::of(16), $result);
}
}
/**
* DooF next = Let name m next
- * | In [name] fn
+ * | In [name] fn (m -> next)
*/
interface DooF extends Functor, PatternMatcher
{
<?php
declare(strict_types=1);
+
namespace Widmogrod\Monad\Control\Doo\Algebra;
use Widmogrod\FantasyLand\Functor;
+use function Widmogrod\Functional\compose;
class In implements DooF
{
private $names;
private $fn;
+ private $next;
- public function __construct(array $names, callable $fn)
+ public function __construct(array $names, callable $fn, callable $next)
{
$this->names = $names;
$this->fn = $fn;
+ $this->next = $next;
}
/**
{
return new self(
$this->names,
- $this->fn
+ $this->fn,
+ compose($function, $this->next)
);
}
*/
public function patternMatched(callable $fn)
{
- return $fn($this->names, $this->fn);
+ return $fn($this->names, $this->fn, $this->next);
}
}
<?php
declare(strict_types=1);
+
namespace Widmogrod\Monad\Control\Doo;
use Widmogrod\FantasyLand\Monad;
function in(array $names, callable $fn): MonadFree
{
- return liftF(new Algebra\In($names, $fn));
+ return liftF(new Algebra\In($names, $fn, Pure::of));
}
});
});
},
- In::class => function (array $names, callable $fn): Reader {
- return Reader::of(function (Registry $registry) use ($names, $fn) {
+ In::class => function (array $names, callable $fn, callable $next): Reader {
+ return Reader::of(function (Registry $registry) use ($names, $fn, $next) {
$args = array_map(function ($name) use ($registry) {
return $registry->get($name);
}, $names);
- return Pure::of($fn(...$args));
+ return $next($fn(...$args));
});
},
], $f);