Created
March 12, 2014 23:15
-
-
Save dalaing/9518669 to your computer and use it in GitHub Desktop.
There and back again
This file contains 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
import Data.Profunctor | |
import qualified Data.Set as S | |
import Control.Monad.Trans (liftIO) | |
import Control.Monad.Trans.State | |
type MyMonad r = StateT (S.Set Int) IO r | |
runMyMonad :: MyMonad r -> IO r | |
runMyMonad m = evalStateT m S.empty | |
newtype ID = ID Int | |
create :: Int -> MyMonad ID | |
create x = do | |
modify (S.insert x) | |
liftIO ∘ print $ x | |
get >>= liftIO ∘ print | |
return (ID x) | |
destroy :: ID -> MyMonad () | |
destroy (ID x) = do | |
modify (S.delete x) | |
liftIO ∘ print $ x | |
get >>= liftIO ∘ print | |
wrap :: Int -> MyMonad () -> MyMonad () | |
wrap x a = dimap create (>>= destroy) (λb -> b >>= λy -> a >> return y) x | |
testFold :: [Int] -> MyMonad() -> MyMonad () | |
testFold xs a = foldr wrap a xs | |
testAction :: MyMonad () | |
testAction = liftIO (putStrLn "Done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment