Example #1
0
 public function provideFunctorTestData()
 {
     return ['IO' => ['$f' => function ($x) {
         return $x + 1;
     }, '$g' => function ($x) {
         return $x + 5;
     }, '$x' => IO::of(function () {
         return 1;
     })]];
 }
Example #2
0
/**
 * 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());
}
Example #3
0
/**
 * doo :: State IO m => [m a] -> m a
 *
 * Haskell like "do notation" simple implementation.
 * Since "do" is reserved keyword in PHP then I use "doo".
 *
 * @param array|M\IO[] $monads
 *
 * @return M\IO
 */
function doo(array $monads)
{
    return M\IO::of(function () use($monads) {
        $result = null;
        $data = [];
        // TODO do it by foldWithKeys
        foreach ($monads as $key => $monad) {
            // TODO do it better - maybe?
            if ($monad instanceof M\IO) {
                $monad = ioState($monad);
            }
            $state = [$key, $data];
            list($result, list(, $data)) = M\State\runState($monad, $state);
        }
        return $result;
    });
}
Example #4
0
/**
 * getEnv :: String -> IO String
 *
 * @param string $name
 *
 * @throws IOError
 *
 * @return M\IO
 */
function getEnv($name)
{
    return M\IO::of(function () use($name) {
        $value = \getenv($name);
        if (false === $value) {
            throw userError(sprintf('Environment variable "%s" does not exists.', $name));
        }
        return $value;
    });
}