Created
March 24, 2014 18:18
-
-
Save ryanlecompte/9745983 to your computer and use it in GitHub Desktop.
lazily recurse files in a root directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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