Beispiel #1
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);
 }
Beispiel #2
0
 private function searchTree(callable $f, Tree $tree)
 {
     if ($tree instanceof EmptyTree) {
         return mzero();
     }
     if ($f($tree->value)) {
         return $this->searchTree($f, $tree->left)->mplus(ret($tree->value))->mplus($this->searchTree($f, $tree->right));
     } else {
         return $this->searchTree($f, $tree->left)->mplus($this->searchTree($f, $tree->right));
     }
 }
Beispiel #3
0
 public function testMonoid()
 {
     $ret = ret('ab')->mappend(ret('cd'));
     $this->assertInstanceOf('Laiz\\Func\\Any', $ret);
     $this->assertEquals('abcd', $ret->cast(''));
     $this->assertEquals(['ab', 'cd'], $ret->cast([]));
     $this->assertEquals(Just('abcd'), $ret->cast(Nothing()));
     $this->assertEquals(['ab', 'cd', 'x'], $ret->mappend(['x']));
     $this->assertEquals(Just('abcdx'), $ret->mappend(Just('x')));
     $e = mempty();
     $s = 'str';
     $a = ['arr'];
     $m = Just('m');
     $this->assertEquals('str', mappend($e, $s));
     $this->assertEquals('str', mappend(mappend($e, $s), $e));
     $this->assertEquals(['arr'], mappend($e, $a));
     $this->assertEquals(['arr'], mappend(mappend($e, $a), $e));
     $this->assertEquals(Just('m'), mappend($e, $m));
     $this->assertEquals(Just('m'), mappend(mappend($e, $m), $e));
 }