public function test_it_should_evaluate_without_arguments_if_all_needed_were_defined() { $sum = f\curryN(0, function ($a, $b) { return $a + $b; }, [1, 2]); $this->assertSame(3, $sum()); }
public function test_it_should_sum_all_from_one_list_with_single_element() { // sum <$> [1, 2] <*> [4, 5] $sum = f\curryN(2, 'example\\sum'); $a = Listt::of([1, 2]); $b = Listt::of([4, 5]); $result = f\map($sum, $a)->ap($b); $this->assertEquals(Listt::of([5, 6, 6, 7]), $result); }
/** * @dataProvider provideData */ public function test_it_should_extract_elements_which_exists($data) { // $get :: String a -> [b] -> Maybe b $get = f\curryN(2, function ($key, $array) { return isset($array[$key]) ? m\just($array[$key]) : m\nothing(); }); $listOfFirstImages = f\pipeline(Listt::of, f\map(m\maybeNull), f\bind(f\bind($get('meta'))), f\bind(f\bind($get('images'))), f\bind(f\bind($get(0))), f\join); $result = $listOfFirstImages($data); $this->assertEquals(Listt::of([m\just('//first.jpg'), m\just('//third.jpg'), m\nothing()]), $result); }
/** * tryCatch :: Exception e => IO a -> (e -> IO a) -> IO a * * @param M\IO $io * @param callable $catchFunction * * @return M\IO */ function tryCatch(M\IO $io = null, callable $catchFunction = null) { return call_user_func_array(f\curryN(2, function (M\IO $io, callable $catchFunction) { return M\IO::of(function () use($io, $catchFunction) { try { return $io->run(); } catch (\Exception $e) { return call_user_func($catchFunction, $e); } }); }), func_get_args()); }
/** * Generic test to verify if a type obey the applicative laws. * * @param callable $assertEqual Asserting function (Applicative $a1, Applicative $a2, $message) * @param callable $pure Applicative "constructor" * @param Applicative $u Applicative f => f (a -> b) * @param Applicative $v Applicative f => f (a -> b) * @param Applicative $w Applicative f => f (a -> b) * @param callable $f (a -> b) * @param mixed $x Value to put into a applicative */ public static function test(callable $assertEqual, callable $pure, Applicative $u, Applicative $v, Applicative $w, callable $f, $x) { // identity: pure id <*> v = v $assertEqual($pure(f\identity)->ap($v), $v, 'identity'); // homomorphism: pure f <*> pure x = pure (f x) $assertEqual($pure($f)->ap($pure($x)), $pure($f($x)), 'homomorphism'); // interchange: u <*> pure x = pure ($ x) <*> u $assertEqual($u->ap($pure($x)), $pure(f\applicator($x))->ap($u), 'interchange'); // composition: pure (.) <*> u <*> v <*> w = u <*> (v <*> w) $compose = f\curryN(2, f\compose); $assertEqual($pure($compose)->ap($u)->ap($v)->ap($w), $u->ap($v->ap($w)), 'composition'); }
/** * Open $maybe monad * * fromMaybe :: a -> Maybe a -> a * * @param mixed $default * @param Maybe $maybe * * @return mixed */ function fromMaybe($default = null, Maybe $maybe = null) { return call_user_func_array(f\curryN(2, function ($default, Maybe $maybe) { return maybe($default, f\identity, $maybe); }), func_get_args()); }
/** * @dataProvider provideSetoidLaws */ public function test_it_should_obay_monoid_laws($a, $b, $c) { MonoidLaws::test(f\curryN(3, [$this, 'assertEquals']), $a, $b, $c); }
/** * @dataProvider provideRandomizedData */ public function test_it_should_obey_monoid_laws(Monoid $x, Monoid $y, Monoid $z) { MonoidLaws::test(f\curryN(3, [$this, 'assertEquals']), $x, $y, $z); }
/** * @dataProvider provideFunctorTestData */ public function test_it_should_obey_functor_laws(callable $f, callable $g, Functor $x) { FunctorLaws::test(f\curryN(3, [$this, 'assertEquals']), $f, $g, $x); }
/** * Adapt function that may throws exceptions to Either monad. * * tryCatch :: Exception e => (a -> b) -> (e -> c) -> a -> Either c b * * @param callable $function (a -> b) * @param callable $catchFunction (e -> c) * @param mixed $value a * * @return Either|\Closure */ function tryCatch(callable $function = null, callable $catchFunction = null, $value = null) { return call_user_func_array(f\curryN(3, function (callable $function, callable $catchFunction, $value) { return f\tryCatch(f\compose(right, $function), f\compose(left, $catchFunction), $value); }), func_get_args()); }
/** * @dataProvider provideApplicativeTestData */ public function test_it_should_obey_applicative_laws($pure, Applicative $u, Applicative $v, Applicative $w, callable $f, $x, $state) { ApplicativeLaws::test(function (State $a, State $b, $message) use($state) { $this->assertEquals($a->runState($state), $b->runState($state), $message); }, f\curryN(1, $pure), $u, $v, $w, $f, $x); }