Josh Suereth
unread,Mar 17, 2012, 10:06:29 AM3/17/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Sign in to report message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to shairon...@gmail.com, Daniel Sobral, √iktor Ҡlang, scala...@googlegroups.com
I'm running into naming conflics here. Let's try again:
object files extends Traversable[File] {
def foreach[U](f: File => U) = {
def walk(file: File): Unit =
if(file.isDirectory) {
f(file)
for {
nextfiles <- Option(file.listFiles)
nextfile <- nextfiles
} walk(nextfile)
} else f(file)
walk(new File(path))
}
}
Note: That version of foreach has stack issues. I'm mostly trying to show you how creating a *collection* of files can be really simple. You can make your own "files" collection for different traversal strategies (depth-first, breadth-first) etc. Dealing with a collection of files should give you access to all the fun powers of collections in Scala, like map/flatMap/filter/groupBy etc.
So, hopefully, I showed you enough of walk to get the gist and write your own custom versions. In my experience, breadth-first search is *usually* a better option for these file traversables.
Hope that helps!
- Josh