Skip to content

Instantly share code, notes, and snippets.

@fatho
Last active August 29, 2015 14:06
Show Gist options
  • Save fatho/246bc2d7f984246d9ac0 to your computer and use it in GitHub Desktop.
Save fatho/246bc2d7f984246d9ac0 to your computer and use it in GitHub Desktop.
ListT using conduit
import Control.Applicative
import Control.Monad
import Data.Conduit
import qualified Data.Conduit.List as CL
newtype ListT m a = ListT { runListT :: Producer m a }
instance (Monad m) => Functor (ListT m) where
fmap f (ListT l) = ListT (l $= CL.map f)
instance (Monad m) => Applicative (ListT m) where
pure = return
(<*>) = ap
instance (Monad m) => Monad (ListT m) where
return x = ListT $ toProducer $ yield x
l >>= f = ListT $ runListT l $= awaitForever (\x -> runListT $ f x)
each :: (Monad m) => [a] -> ListT m a
each xs = ListT $ CL.sourceList xs
asList :: (Monad m) => ListT m a -> m [a]
asList l = runListT l $$ CL.consume
foo :: ListT IO Int
foo = do
x <- each [1..10]
y <- each [1..10]
return (x + y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment