我并不是哈斯克尔的新手,但在现实世界中并没有太多用处。如何在haskell的文件系统中实现搜索?
所以我想要做的是从一些文件夹开始找到所有的git仓库。基本上我试图通过使用哈斯克尔并发功能来更快地完成这个工作find . -type d -exec test -e '{}/.git' ';' -print -prune
。
这就是我到目前为止。
import Control.Concurrent.Async
import System.Directory (doesDirectoryExist)
import System.FilePath ((</>))
import System.IO (FilePath)
isGitRepo :: FilePath -> IO Bool
isGitRepo p = doesDirectoryExist $ p </> ".git"
main :: IO()
main = putStrLn "hello"
我发现这个lib具有这种功能mapConcurrently :: Traversable t => (a -> IO b) -> t a -> IO (t b)
这让我想,我需要的是产生懒树数据结构,将反映的文件夹结构。然后与isGitRepo
同时过滤,并将其折叠到列表中并打印出来。 那么,我当然知道如何制作data FTree = Node String [FTree]
或类似的东西,但我有问题。 如何同时生产?如何在遍历树时生成绝对路径?像那样的问题等等。
嘿!如果我想在可用时打印结果怎么办?有了这个版本,它只会在最后打印整个列表(比找到实际更快,这是好的,但仍然)。我可以使用渠道创建解决方案,但也许有更简单的方法? – user1685095
@ user1685095我想一个频道可能是要走的路......或者,你可以使用一些预先构建的东西。快速搜索产生https://hackage.haskell.org/package/concurrent-output-1.7.8/docs/System-Console-Concurrent.html – Alec