Skip to content

Instantly share code, notes, and snippets.

@bgamari
Created July 27, 2015 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgamari/9623997162a3399859a9 to your computer and use it in GitHub Desktop.
Save bgamari/9623997162a3399859a9 to your computer and use it in GitHub Desktop.
module Main where
import Control.Monad hiding (mapM_)
import Prelude hiding (mapM_)
-- | Testcase derived from Assembler monad in ByteCodeAsm
data Assembler a
= Thing Int (Int -> Assembler a)
| Pure a
instance Functor Assembler where
fmap = liftM
instance Applicative Assembler where
pure = return
(<*>) = ap
instance Monad Assembler where
return = Pure
Pure x >>= f = f x
Thing i k >>= f = Thing i (k >=> f)
mapA_ :: (Foldable t, Monad f) => (a -> f b) -> t a -> f ()
mapA_ f = foldr ((*>) . f) (pure ())
mapM_ :: (Foldable t, Monad f) => (a -> f b) -> t a -> f ()
mapM_ f = foldr ((>>) . f) (pure ())
test = map (\i->Thing i (const $ return 2)) [0..10000]
doTestM = mapM_ id test
doTestA = mapA_ id test
run :: Assembler a -> a
run (Thing i f) = run (f i)
run (Pure r) = r
{-# NOINLINE run #-}
main :: IO ()
main = print $ run doTestM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment