예제 #1
0
 public static function fmap(callable $f, $a)
 {
     assert($a instanceof Instance, 'Second argument must be Writer');
     return bind($a, function ($inner) use($f) {
         return Monad\Writer::ret($f($inner));
     });
 }
예제 #2
0
 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);
 }