patternMinor
Processing files in a directory tree
Viewed 0 times
processingfilestreedirectory
Problem
I'm new to Scala, and would appreciate any notes about a better approach, correctness, or style.
/**
* Call proc(f) for each file in the directory tree rooted at dir.
*
* proc will not be called for directories, just for files.
*/
def traverse(dir: File, proc: File => Unit): Unit = {
dir.listFiles foreach { (f) => {
if(f.isDirectory) {
traverse(f, proc)
} else {
proc(f)
}
}
}
}Solution
Your code is fine. Nevertheless, removing unnecessary braces can increase readability:
def traverse(dir: File, proc: File => Unit): Unit =
dir.listFiles foreach { f => if (f.isDirectory) traverse(f, proc) else proc(f) }Code Snippets
def traverse(dir: File, proc: File => Unit): Unit =
dir.listFiles foreach { f => if (f.isDirectory) traverse(f, proc) else proc(f) }Context
StackExchange Code Review Q#37106, answer score: 3
Revisions (0)
No revisions yet.