Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save danidiaz/7713166 to your computer and use it in GitHub Desktop.
Using applicatives to track the number of steps in a computation.
module Main where
import Data.Monoid
import Control.Applicative
import Data.Functor.Compose
type SteppedIO a = Compose ((,) (Sum Int)) IO a
step :: IO a -> SteppedIO a
step cmd = Compose (Sum 1, cmd)
countSteps :: SteppedIO a -> Int
countSteps = getSum . fst . getCompose
exec :: SteppedIO a -> IO a
exec = 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