Skip to content

Instantly share code, notes, and snippets.

@danidiaz
Created November 29, 2013 23:57
Show Gist options
  • Select an option

  • Save danidiaz/7713554 to your computer and use it in GitHub Desktop.

Select an option

Save danidiaz/7713554 to your computer and use it in GitHub Desktop.
Almost, but not quite. I'm afraid it is imposible to log progress using applicatives.
module Main where
import Data.Monoid
import Control.Applicative
import Data.Functor.Compose
import Control.Monad
import Control.Monad.Trans.State.Strict
type SteppedIO a = Compose ((,) (Sum Int)) (Compose IO (State Int)) a
step :: IO a -> SteppedIO a
step cmd = Compose ( Sum 1, Compose (fmap pure cmd)
<*
Compose (pure (modify succ))
<*
Compose (fmap pure (putStrLn "hi")) )
countSteps :: SteppedIO a -> Int
countSteps = getSum . fst . getCompose
exec :: SteppedIO a -> IO a
exec = fmap (flip evalState 0) . getCompose . snd . getCompose
program :: SteppedIO ()
program =
step (putStrLn "aaa")
*>
step (putStrLn "bbb")
*>
step (putStrLn "ccc")
main :: IO ()
main = do
putStrLn $ "Number of steps: " ++ show (countSteps program)
exec program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment