Skip to content

Instantly share code, notes, and snippets.

@davidpdrsn
Created December 1, 2015 15:01
Show Gist options
  • Select an option

  • Save davidpdrsn/a7da66d8446bfa17314a to your computer and use it in GitHub Desktop.

Select an option

Save davidpdrsn/a7da66d8446bfa17314a to your computer and use it in GitHub Desktop.
module StateIOYo where
import Control.Monad.Trans.State
import Control.Concurrent
import Control.Monad.IO.Class
main :: IO ()
main = do
(_, fs) <- runStateT sampleAction []
evalAll putStrLn fs
evalAll :: (String -> IO ()) -> [SlackResponseRunner] -> IO ()
evalAll f [x] = putStrLn "-- Only one --" >> x f
evalAll f rest = foldr (\x -> (>>) (x f)) (return ()) rest
type SlackResponseRunner = (String -> IO ()) -> IO ()
type BotAction = StateT [SlackResponseRunner] IO ()
sampleAction :: BotAction
sampleAction = do
postToSlack "Starting timer"
postToSlack "Done!"
http :: IO String
http = threadDelay 1000000 >> return "200 OK"
insertIO :: IO () -> BotAction
insertIO action = do
old <- get
let new _ = action
put $ new : old
postToSlack :: String -> BotAction
postToSlack str = do
old <- get
let new actuallyPost = actuallyPost str
put $ new : old
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment