Created
November 29, 2013 23:57
-
-
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.
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 | |
| 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