Created
November 26, 2014 16:57
-
-
Save DeTeam/b47cecfcb82e0bb90f0e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Control.Monad.State (StateT(..), runStateT, modify, get) | |
import Control.Monad.Reader (ReaderT(..), runReaderT, ask) | |
import Control.Monad.Trans.Class (lift) | |
import Control.Monad.IO.Class (liftIO) | |
type MySuperMonad = ReaderT String (StateT Int IO) | |
runMySuperMonad :: String -> Int -> MySuperMonad a -> IO (a, Int) | |
runMySuperMonad username initialBalance m = runStateT stateThing initialBalance | |
where stateThing = runReaderT m username | |
spend :: Int -> MySuperMonad Int | |
spend money = do | |
appName <- ask | |
currentBalance <- get | |
if currentBalance >= money then | |
(logSpending appName) >> modify (\x -> x - money) >> return money | |
else (logError appName) >> return 0 | |
where logSpending appName = liftIO . putStrLn $ "Spent " ++ appName ++ ": " ++ (show money) | |
logError appName = liftIO . putStrLn $ "Error trying to spend " ++ appName ++ ": " ++ (show money) | |
performSpendings = do | |
spend 100 | |
spend 100 | |
spend 1000 | |
performLotsOfSTuff = do | |
performSpendings | |
performSpendings | |
performSpendings | |
performSpendings | |
main :: IO () | |
main = do | |
print =<< runMySuperMonad "Vasiliy" 100 performLotsOfSTuff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
no no no david blaine)