Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ryanlecompte
Created March 24, 2014 18:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanlecompte/9745983 to your computer and use it in GitHub Desktop.
Save ryanlecompte/9745983 to your computer and use it in GitHub Desktop.
lazily recurse files in a root directory
import java.io.File
/**
* Iterate all files in the given directory recursively.
* @param root the root directory to traverse
* @return an Iterator[File] of traversed files
*/
def listFiles(root: File): Iterator[File] = {
def rec(files: List[File]): Stream[File] = {
files match {
case h :: rest =>
if (h.isFile) h #:: rec(rest)
else rec(h.listFiles.toList) #::: rec(rest)
case Nil => Stream.empty
}
}
rec(root.listFiles.toList).iterator
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment