Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 哈斯克尔有目录查询器吗?_Haskell_Io_Directory Traversal - Fatal编程技术网

Haskell 哈斯克尔有目录查询器吗?

Haskell 哈斯克尔有目录查询器吗?,haskell,io,directory-traversal,Haskell,Io,Directory Traversal,Haskell中是否有递归目录遍历器,以便我可以编写类似 listing <- walkDir "/tmp" listing具有递归目录遍历功能。这里有一种方法可以列出目录树中的所有Haskell文件,使用不在隐藏目录(其名称以“.”开头)中的文件: 导入数据。可遍历(遍历) 导入System.Directory.Tree( 锚定树(..),直树(..), filterDir,readDirectoryWith ) 导入System.FilePath(takeExtension) lis

Haskell中是否有递归目录遍历器,以便我可以编写类似

listing <- walkDir "/tmp"

listing具有递归目录遍历功能。

这里有一种方法可以列出目录树中的所有Haskell文件,使用不在隐藏目录(其名称以“.”开头)中的文件:

导入数据。可遍历(遍历)
导入System.Directory.Tree(
锚定树(..),直树(..),
filterDir,readDirectoryWith
)
导入System.FilePath(takeExtension)
listFilesDirFiltered=do

_:/tree该软件包为此提供了强大而优雅的功能。例如,它提供了一个函数,该函数将在目录树下递归调用您的函数。例如,我使用它递归地列出目录中从最旧的开始的文件。我有一个递归定义,用于使用filepath包遍历目录:

import Control.Monad
import System.Directory
import System.FilePath
import System.Posix.Files

-- | Traverse from 'top' directory and return all the files by
-- filtering out the 'exclude' predicate.
traverseDir :: FilePath -> (FilePath -> Bool) -> IO [FilePath]
traverseDir top exclude = do
  ds <- getDirectoryContents top
  paths <- forM (filter (not.exclude) ds) $ \d -> do
    let path = top </> d
    s <- getFileStatus path
    if isDirectory s
      then traverseDir path exclude
      else return [path]
  return (concat paths)
import-Control.Monad
导入系统目录
导入System.FilePath
导入System.Posix.xml文件
--|从“top”目录遍历,并按返回所有文件
--筛选出“排除”谓词。
traverseDir::FilePath->(FilePath->Bool)->IO[FilePath]
traverseDir top exclude=do

那个图书馆有教程吗?这看起来并不容易。我在中使用了它,但只使用了hackage上显示的文档。我编写了一个包,它应该可以很容易地定义懒惰的
walkDir
,我想,例如,
Foldable
实例提供的
toList
。它只是库中定义的一个构造函数:我喜欢这个库,它比公认的答案稍微好一点-它更简单,依赖性更少,但仍然同样强大。它也与镜头兼容,但如果你不使用它,它并不依赖于(巨大的)镜头库。这里有一个到主要黑客页面的链接:[编辑:但别误会;FilePather也很优秀]@Trismegistos有一些例子,但Haskell库在实用的教程建议方面往往文档不足,这是事实。我经常发现我在ghci的闲逛比看文件更能让我受益匪浅。谢谢@StephaneRolland。
import Control.Monad
import System.Directory
import System.FilePath
import System.Posix.Files

-- | Traverse from 'top' directory and return all the files by
-- filtering out the 'exclude' predicate.
traverseDir :: FilePath -> (FilePath -> Bool) -> IO [FilePath]
traverseDir top exclude = do
  ds <- getDirectoryContents top
  paths <- forM (filter (not.exclude) ds) $ \d -> do
    let path = top </> d
    s <- getFileStatus path
    if isDirectory s
      then traverseDir path exclude
      else return [path]
  return (concat paths)