işlemek için:scala akışı yinelemeli, dosya ve alt dizinleri
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)
bu çalışır. Önceden answered scala Iteratees kullanarak bu kodu nasıl refactor. Buradaki soru, bu kodu scala Streams kullanarak nasıl yeniden oluşturabilirim. Böyle şey: Burada
val stream: Stream[File] = ... // ???
stream.foreach(exampleFn)
'her dosya' için bir işlev uygulamak - her dosya için, ya her dosya veya-alt dizin için? fn (dosya) 'da alt dizinlere de çağrılmalıdır? –