public static function ap($mf, $a) { if ($mf instanceof Instance\Just) { $f = $mf->fromJust(); return fmap($f, $a); } return new Instance\Nothing(); }
function const1(...$args) { $f = function ($a, $b) { return ap(fmap(cnst(), $a), $b); }; if (count($args) === 2) { return $f(...$args); } else { return f($f, ...$args); } }
public function testFmap() { $m1 = [1, 2, 3]; $m2 = [4, 5, 6]; $ret = fmap(function ($a) use($m2) { return fmap(function ($b) use($a) { return [$a, $b]; }, $m2); }, $m1); $expected = [[[1, 4], [1, 5], [1, 6]], [[2, 4], [2, 5], [2, 6]], [[3, 4], [3, 5], [3, 6]]]; $this->assertEquals($expected, $ret); }
private function opsFold($ret) { foreach ($this->ops as $op) { list($method, $any) = $op; if ($method === self::FMAP) { $ret = fmap($any, $ret); } else { $ret = Loader::callFunction($method, $ret, $any->cast($ret)); } } return $ret; }
/** * fmap (f . g) == fmap f . fmap g */ public function testFunctorLaw2() { $f = f(function ($a) { return "ret {$a}"; }); $g = f(function ($a) { return $a * 3; }); $left = fmap($f->compose($g)); $right = fmap($f)->compose(fmap($g)); $a = f(function ($a) { return $a - 3; }); $this->assertEquals('ret 24', $left($a, 11)); $this->assertEquals($left($a)->apply(11), $right($a)->apply(11)); }
public function testFmap() { $m1 = Just(3); $f1 = function ($a) { return $a + 1; }; $this->assertEquals(Just(4), fmap($f1, $m1)); $f2 = f(function ($a, $b) { return $a + $b; }); $mf = fmap($f2, $m1); $this->assertInstanceOf('Laiz\\Func\\Maybe', $mf); $a = fmap(function ($f) { return $f(4); }, $mf); $this->assertEquals(Just(7), $a); $m2 = Just(2); $this->assertEquals(Just(5), ap($mf, $m2)); }