/** * @dataProvider provideData */ public function test_it_should_extract_elements_which_exists_alternative_solution($data) { // $get :: String a -> Maybe [b] -> Maybe b $get = function ($key) { return f\bind(function ($array) use($key) { return isset($array[$key]) ? m\just($array[$key]) : m\nothing(); }); }; $result = Listt::of($data)->map(Maybe\maybeNull)->bind($get('meta'))->bind($get('images'))->bind($get(0)); $this->assertEquals(Listt::of([m\just('//first.jpg'), m\just('//third.jpg'), m\nothing()]), $result); }
/** * Generic test to verify if a type obey the monad laws. * * @param callable $assertEqual Asserting function (Monad $m1, Monad $m2, $message) * @param callable $return Monad "constructor" * @param callable $f Monadic function * @param callable $g Monadic function * @param mixed $x Value to put into a monad */ public static function test(callable $assertEqual, callable $return, callable $f, callable $g, $x) { // Make reading bellow tests easier $m = $return($x); // left identity: (return x) >>= f ≡ f x $assertEqual(f\bind($f, $return($x)), $f($x), 'left identity'); // right identity: m >>= return ≡ m $assertEqual(f\bind($return, $m), $m, 'right identity'); // associativity: (m >>= f) >>= g ≡ m >>= ( \x -> (f x >>= g) ) $assertEqual(f\bind($g, f\bind($f, $m)), f\bind(function ($x) use($f, $g) { return f\bind($g, $f($x)); }, $m), 'associativity'); }
/** * tryMaybe :: IO a -> IO (Maybe a) * * @param M\IO $io * * @return M\IO */ function tryMaybe(M\IO $io) { return tryCatch(f\bind(M\Maybe\just, $io), M\Maybe\nothing); }
function handleRequest(array $request) { return call_user_func(f\pipeline('validateInput', f\map('canonizeEmail'), f\bind('updateDatabaseStep'), 'sendMessage'), $request); }