Created
November 29, 2013 23:07
-
-
Save danidiaz/7713166 to your computer and use it in GitHub Desktop.
Using applicatives to track the number of steps in a computation.
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
| 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