Skip to content

Instantly share code, notes, and snippets.

@DeTeam
Created November 26, 2014 16:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeTeam/b47cecfcb82e0bb90f0e to your computer and use it in GitHub Desktop.
Save DeTeam/b47cecfcb82e0bb90f0e to your computer and use it in GitHub Desktop.
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
@vyorkin
Copy link

vyorkin commented Nov 29, 2014

no no no david blaine)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment