简体   繁体   English

惯用io-streams目录遍历

[英]Idiomatic io-streams directory traversal

I was discussing some code on Reddit , and it made me curious about how this would be implemented in io-streams. 我正在讨论关于Reddit的一些代码 ,这让我对如何在io-streams中实现它感到好奇。 Consider the following code which traverses a directory structure and prints out all of the filenames: 考虑以下代码遍历目录结构并打印出所有文件名:

import           Control.Exception         (bracket)
import qualified Data.Foldable             as F
import           Data.Streaming.Filesystem (closeDirStream, openDirStream,
                                            readDirStream)
import           System.Environment        (getArgs)
import           System.FilePath           ((</>))

printFiles :: FilePath -> IO ()
printFiles dir = bracket
    (openDirStream dir)
    closeDirStream
    loop
  where
    loop ds = do
        mfp <- readDirStream ds
        F.forM_ mfp $ \fp' -> do
            let fp = dir </> fp'
            ftype <- getFileType fp
            case ftype of
                FTFile -> putStrLn fp
                FTFileSym -> putStrLn fp
                FTDirectory -> printFiles fp
                _ -> return ()
            loop ds

main :: IO ()
main = getArgs >>= mapM_ printFiles

Instead of simply printing the files, suppose we wanted to create some kind of streaming filepath representation. 而不是简单地打印文件,假设我们想要创建某种流式文件路径表示。 I know how this would work in enumerator, conduit, and pipes. 我知道这在枚举器,管道和管道中是如何工作的。 However, since the intermediate steps require acquisition of a scarce resource (the DirStream ), I'm not sure what the implementation would be for io-streams. 但是,由于中间步骤需要获取稀缺资源( DirStream ),我不确定io-stream的实现方式。 Can someone provide an example of how that would be done? 有人能举例说明如何做到这一点吗?

For comparison, here's the conduit implementation , which is made possible via bracketP and MonadResource . 为了比较, 这里是管道实现 ,可以通过bracketPMonadResource And here's how the conduit code would be used to implemented the same file printing program as above: 以下是管道代码如何用于实现上述相同的文件打印程序:

import           Control.Monad.IO.Class       (liftIO)
import           Control.Monad.Trans.Resource (runResourceT)
import           Data.Conduit                 (($$))
import           Data.Conduit.Filesystem      (sourceDirectoryDeep)
import qualified Data.Conduit.List            as CL
import           System.Environment           (getArgs)

main :: IO ()
main =
    getArgs >>= runResourceT . mapM_ eachRoot
  where
    -- False means don't traverse dir symlinks
    eachRoot root = sourceDirectoryDeep False root
                 $$ CL.mapM_ (liftIO . putStrLn)

Typical style would be to do something like this: 典型的风格是做这样的事情:

traverseDirectory :: RawFilePath -> (InputStream RawFilePath -> IO a) -> IO a

ie a standard "with-" function, with the obvious implementation. 即标准的“with-”功能,具有明显的实现。

Edit : added a working example implementation: https://gist.github.com/gregorycollins/00c51e7e33cf1f9c8cc0 编辑 :添加了一个工作示例实现: https//gist.github.com/gregorycollins/00c51e7e33cf1f9c8cc0

It's not exactly complicated but it's also not as trivial as I had first suggested. 它并不完全复杂,但它也不像我最初建议的那样微不足道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM