Esempio n. 1
0
 /**
  * Generic test to verify if a type obey the functor laws.
  *
  * @param callable $assertEqual Asserting function (Functor $f1, Functor $f2, $message)
  * @param callable $f   (a -> b)
  * @param callable $g   (a -> b)
  * @param Functor $x    f a
  */
 public static function test(callable $assertEqual, callable $f, callable $g, Functor $x)
 {
     // identity: fmap id  ==  id
     $assertEqual(f\map(f\identity, $x), $x, 'identity');
     // composition: fmap (f . g)  ==  fmap f . fmap g
     $assertEqual(f\map(f\compose($f, $g), $x), call_user_func(f\compose(f\map($f), f\map($g)), $x), 'composition');
 }
Esempio n. 2
0
 public function test_it_should_retun_function_accepting_arguments()
 {
     $this->assertInstanceOf(\Closure::class, f\compose('strtolower', 'strtoupper'));
 }
Esempio n. 3
0
/**
 * 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());
}