1

pipes 教程有以下示例。这段代码在 4.1.1 版中会是什么样子?

  read' :: FilePath -> Frame Text IO C C ()
  read' file = do
      liftU $ putStrLn "Opening file..."
      h <- liftU $ openFile file ReadMode
      -- The following requires "import qualified Control.Monad as M"
      finallyD (putStrLn "Closing file ..." M.>> hClose h) $ readFile' h
4

1 回答 1

2

等效函数是readFilefrom Pipes.Safe.Prelude,您可以在此处找到。我已粘贴以下来源以供参考:

withFile :: MonadSafe m => FilePath -> IO.IOMode -> (IO.Handle -> m r) -> m r
withFile file ioMode = bracket (liftIO $ IO.openFile file ioMode) (liftIO . IO.hClose)

readFile :: MonadSafe m => FilePath -> Producer' String m ()
readFile file = withFile file IO.ReadMode P.fromHandle
于 2014-04-20T04:29:13.693 回答