如何用bracketP替换括号的使用方法

时间:2017-04-02 16:24:46

标签: haskell stream conduit

以下代码编译。

result :: IO Editor
result = 
  bracket h (\h' -> hClose h' >> putStrLn "closing file") (\h' -> engineOne h')
  where
    h :: IO Handle
    h = do h' <- openFile "data/tc_one.data" ReadMode
           _  <- hGetLine h'
           return h'

engineOne :: Handle -> IO Editor
engineOne h = evalStateT conduit nullEDT
  where conduit = runConduit   $ 
                  sourceHandle h .|
                  parsed         .|
                  command

但是我想知道我是否在追求空间泄漏,也许我应该使用bracketP。所以我试试:

dropLine :: MonadResource m => FilePath -> ConduitM () ByteString m ()
dropLine fp = bracketP h hClose sourceHandle
  where 
    h :: IO Handle
    h = do h' <- openBinaryFile fp ReadMode
           _  <- hGetLine h'
           return h'

本身,它编译。<​​/ p>

但是当我把它放在管道中时

engineTwo :: FilePath -> IO Editor
engineTwo fp = evalStateT conduit nullEDT
  where conduit = runConduit $ dropLine fp .| parsed .| command

我收到此错误

Editor.hs:54:32: error:
    • No instance for (MonadResource IO)
        arising from a use of ‘dropLine’
    • In the first argument of ‘(.|)’, namely ‘dropLine fp’
      In the second argument of ‘($)’, namely
        ‘dropLine fp .| parsed .| command’
      In the expression: runConduit $ dropLine fp .| parsed .| command

以下是其他类型的签名:

command :: Sink (Int,Text) (StateT Editor IO) Editor
command = undefined

parsed :: Conduit ByteString (StateT Editor IO) (Int,Text)
parsed = undefined

所以问题似乎是在IO monad中使用StateT。但我不知道该怎么办。有什么想法吗?

0 个答案:

没有答案