Created
December 18, 2017 14:07
-
-
Save ssanj/a59716b4656e76cac2fa6d47be97e6d2 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
{-# LANGUAGE TypeSynonymInstances #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
module Sample(perform, runAsIO, runAsSt) where | |
import Text.Printf (printf) | |
import qualified Control.Monad.Trans.State as S | |
class Monad m => ConsoleR m where | |
writeLine :: String -> m () | |
readLine :: m String | |
perform :: ConsoleR m => m () | |
perform = do writeLine "What is your name?" | |
name <- readLine | |
writeLine (printf "Hello %s" name) | |
instance ConsoleR IO where | |
writeLine = putStrLn | |
readLine = getLine | |
instance ConsoleR (S.State String) where | |
writeLine l = S.modify (++ "WL:" ++ l) | |
readLine = S.state (\s -> ("some_name", s ++ "RL:some_name")) | |
runAsIO :: IO () | |
runAsIO = perform :: IO () | |
runAsSt :: S.State String () | |
runAsSt = perform :: S.State String () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment