예제 #1
0
function retrieveRelated($productName)
{
    return S::of(function (Cacher $cache) use($productName) {
        // do some database work
        $products = ['iPhone 5', 'iPhone 6s'];
        return [$products, $cache->put($productName, $products)];
    });
}
예제 #2
0
/**
 * ioState :: IO a -> State IO a
 *
 * @param M\IO $io
 *
 * @return M\State
 */
function ioState(M\IO $io)
{
    return M\State::of(function ($state) use($io) {
        list($key, $data) = $state;
        $value = $io->run();
        $data[$key] = $value;
        $newState = [$key, $data];
        return [$value, $newState];
    });
}
예제 #3
0
/**
 * modify :: State s m => (s -> s) -> m ()
 *
 * Monadic state transformer.
 *
 * Maps an old state to a new state inside a state monad.
 * The old state is thrown away.
 *
 * @param callable $transformation
 *
 * @return M\State
 */
function modify(callable $transformation)
{
    return M\State::of(function ($state) use($transformation) {
        return [null, $transformation($state)];
    });
}