public function testRun() { $ret = runWriter(tell("a")->bind(function ($_) { return Monad\Writer::ret(1); })); $this->assertEquals([1, "a"], $ret); $ret = runWriter(tell("a")->bind(function ($_) { return ret(1); // Any#cast => Writer })); $this->assertEquals([1, "a"], $ret); $ret = runWriter(tell("a")->bind(function ($_) { return tell("b"); })->bind(function ($_) { return ret(2); })); $this->assertEquals([2, "ab"], $ret); $ret = runWriter(tell("a")->bind(function ($_) { return tell("b"); })->bind(function ($_) { return ret(2); })->bind(function ($a) { return ret(3); })); $this->assertEquals([3, "ab"], $ret); $ret = runWriter(ret(2)->bind(function ($_) { return tell("a"); })->bind(function ($_) { return ret(2); })); $this->assertEquals([2, "a"], $ret); }
public function testWriterMonad() { $ret = runWriter(ret(1)->bind(function ($_) { return ret(2); })->bind(function ($a) { return tell("a")->bind(function ($_) use($a) { return ret($a + 5); }); })); $this->assertEquals([7, "a"], $ret); }