我想申请一个功能在目录和子目录中的每个文件,内容如下:阶流处理递归文件和子目录
def applyRecursively(dir: String, fn: (File) => Any) {
def listAndProcess(dir: File) {
dir.listFiles match {
case null => out.println("exception: dir cannot be listed: " + dir.getPath); List[File]()
case files => files.toList.sortBy(_.getName).foreach(file => {
fn(file)
if (file.isDirectory) listAndProcess(file)
})
}
}
listAndProcess(new File(dir))
}
def exampleFn(file: File) { println(s"processing $file") }
applyRecursively(dir, exampleFn)
这个工程。以前他们answered如何通过使用scala迭代器来重构此代码。这里的问题是我如何通过使用scala Streams来重构此代码。 是这样的:
val stream: Stream[File] = ... // ???
stream.foreach(exampleFn)
'应用功能,每个文件' - 为每个文件或每个文件有或子目录?应该在子目录上调用'fn(file)'? –