1
1
mirror of https://github.com/thma/LtuPatternFactory.git synced 2024-12-03 03:55:08 +03:00

stack IO with WriterT

This commit is contained in:
thma 2019-02-03 16:41:06 +01:00
parent 1c512337f9
commit eeebfe187b

View File

@ -2,31 +2,32 @@ module Command where
import Control.Monad.Writer
data Light = Light {
turnOn :: String
, turnOff :: String
turnOn :: IO String
, turnOff :: IO String
}
simpleLamp = Light {
turnOn = "The Light is on"
, turnOff = "The Light is off"
turnOn = putStrLn "The Light is on" >> return "on"
, turnOff = putStrLn "The Light is off" >> return "off"
}
flipUpCommand :: Light -> String
flipUpCommand :: Light -> IO String
flipUpCommand = turnOn
flipDownCommand :: Light -> String
flipDownCommand :: Light -> IO String
flipDownCommand = turnOff
storeAndExecute :: String -> Writer[String] ()
storeAndExecute :: IO String -> WriterT[String] IO ()
storeAndExecute command = do
let logEntry = command
logEntry <- liftIO command
tell [logEntry]
commandDemo :: IO ()
commandDemo = do
let lamp = simpleLamp
print $ execWriter $
result <- execWriterT $
storeAndExecute (flipUpCommand lamp) >>
storeAndExecute (flipDownCommand lamp) >>
storeAndExecute (flipUpCommand lamp)
putStrLn $ "switch history: " ++ show result