Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
network-conduit vs monad transformer
{-# LANGUAGE PackageImports, TypeFamilies, FlexibleContexts #-}
-- code experiments with using monad stack with conduit
-- it works well except one case - using ResourceT in inner
-- monad (commented out function)
import Data.Conduit
import Data.Conduit.Network
import "mtl" Control.Monad.Reader
import "mtl" Control.Monad.Writer
import "mtl" Control.Monad.State
import Control.Monad.Trans.Resource (register)
data SInit = SInit Int deriving (Show,Eq)
data SState = SState Int deriving (Show,Eq)
main :: IO ()
main = do
let r = SInit 42
s = SState 3
flip runReaderT r $
flip runStateT s $
runTCPServer (ServerSettings 3500 Nothing) app
return ()
app ::
(Base m ~ IO,
MonadReader SInit m,
MonadIO m,
IsSource src,
Resource m
) =>
src m a1 -> Sink a1 m b -> ResourceT m b
app src snk = src $= (process1 =$= process2 {-=$= process3-}) $$ snk
where
process1 = self
where
self = Conduit push close
push item = do
r <- lift ask
liftIO $ print r
register $ putStrLn "freed"
return $ Producing self [item]
close = return []
process2 = self
where
self = Conduit push close
push item = do
r <- lift ask
-- using reader, ok
(k,t) <- runWriterT $ do {- 1 -}
x <- (lift . lift) ask
tell [x]
return ()
-- using reader and IO
(k,t) <- runWriterT $ do {- 2 -}
(SInit x) <- (lift . lift) ask
liftIO $ print $ x + 1
tell [x]
-- using reader, IO and register, failed
(k,t) <- runWriterT $ do {- 3 -}
(SInit x) <- (lift . lift) ask
liftIO $ print $x+1
lift $ register $ print "freed2"
tell [x]
return $ Producing self [item]
close = return []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.