本文介绍了如何使用Conduit保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用导管库来保存文件?

  main :: IO()我的使用案例是通过管道教程查看的,但似乎无法找到任何东西, 
main = do
xxs< - lines <$> (readFile filePath)
sourceList xxs = $ pipe $$ saveFile

pipe :: Monad m => Conduit String m String
pipe = undefined

所以这里有两个问题:


  1. 使用将字符串转换为$列表是否有意义b $ b字符串,然后将它提供给 sourceList


  2. 如何实现 saveFile 函数,这样当字符串
    xxs 被完全处理时,我可以将它写入磁盘? conduit library:

     #!/ usr / bin / env stack 
    { - stack
    --resolver lts-6.7
    --install-ghc
    runghc
    - 软件包管道 - 额外
    - 软件包资源
    --package conduit
    - }

    导入Data.Conduit.Binary(sinkFile,sourceFile)
    导入Control.Monad.Trans.Resource
    导入数据。 ($ $$),await,Conduit,(= $),yield)
    导入Data.Monoid((<>))
    导入Control.Monad.IO.Class

    myConduit = do
    str< - 等待
    大小写
    只需x - >做
    liftIO $打印一些处理
    收益x
    myConduit
    无 - > return()


    saveFile :: FilePath - > FilePath - > IO()
    saveFile f1 f2 = runResourceT $ sourceFile f1 $$ myConduit = $ sinkFile f2

    main :: IO()
    main = saveFiletest.txtatest .txt



    您可以在 myConduit 函数中实现该功能。请注意,在您的示例中,您正在使用 readFile 函数调用,它将懒惰地读取文件。 Conduit提供它自己的读取和写入文件的抽象,你应该使用它。


    How do you save a file using conduit's library? I looked through conduit's tutorial but can't seem to find anything, here is my use case:

    main :: IO ()
    main = do
      xxs  <- lines <$> (readFile filePath)
      sourceList xxs =$ pipe $$ saveFile
    
    pipe :: Monad m => Conduit String m String
    pipe = undefined
    

    So there's two question here:

    1. Does it make sense to use lines to convert a string into a list ofstrings and then feeding it to sourceList?

    2. How should I implement the saveFile function so that when the stringsxxs are fully processed, I can write it to disk?

    解决方案

    A small minimal example of what you are trying to do using the conduit library:

    #!/usr/bin/env stack
    {- stack
         --resolver lts-6.7
         --install-ghc
         runghc
         --package conduit-extra
         --package resourcet
         --package conduit
     -}
    
    import Data.Conduit.Binary (sinkFile, sourceFile)
    import Control.Monad.Trans.Resource
    import Data.Conduit (($$), await, Conduit, (=$), yield)
    import Data.Monoid ((<>))
    import Control.Monad.IO.Class
    
    myConduit = do
      str <- await
      case str of
        Just x -> do
                  liftIO $ print "some processing"
                  yield x
                  myConduit
        Nothing -> return ()
    
    
    saveFile :: FilePath -> FilePath -> IO ()
    saveFile f1 f2 = runResourceT $ sourceFile f1 $$ myConduit =$ sinkFile f2
    
    main :: IO ()                 
    main = saveFile "test.txt" "atest.txt"
    

    You implement that in your myConduit function. Note that in your example you are using readFile function call which will read the file lazily. Conduit provides it's own abstraction for reading and writing files, you should use that.

    这篇关于如何使用Conduit保存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 02:21