Skip to content

Instantly share code, notes, and snippets.

@dalaing
Created March 12, 2014 23:15
Show Gist options
  • Save dalaing/9518669 to your computer and use it in GitHub Desktop.
Save dalaing/9518669 to your computer and use it in GitHub Desktop.
There and back again
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