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)]; }); }
/** * 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]; }); }
/** * 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)]; }); }