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]; }); }
/** * runState :: State s a -> s -> (a, s) * * Unwrap a state monad computation as a function. * * @param M\State $state * @param mixed $initialState * * @return mixed */ function runState(M\State $state, $initialState) { return $state->runState($initialState); }