A visitor is truly foreachwithout a function, so let's do it foreach. The method is static, but it takes a as the first argument Path, so we enrich it Pathwith a method foreachthat is executed using the following:
import java.nio.file._
import java.nio.file.attribute.BasicFileAttributes
implicit def fromNioPath(path: Path): TraverseFiles = new TraversePath(path)
And everything else is inside the class TraversePath, which looks something like this:
class TraversePath(path: Path) {
def foreach(f: (Path, BasicFileAttributes) => Unit) {
// ...
}
}
, :
ProjectHome foreach ((file, _) => if (!file.toString.contains(".svn")) println(File))
, , - :
class TraversePath(path: Path) {
def foreach(f: (Path, BasicFileAttributes) => Unit) {
class Visitor extends SimpleFileVisitor[Path] {
override def visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult = try {
f(file, attrs)
FileVisitResult.CONTINUE
} catch {
case _ => FileVisitResult.TERMINATE
}
}
Files.walkFileTree(path, new Visitor)
}
}
, , ! . , foreach - , Traversable, Scala!
, Traversable.foreach , . . :
import java.nio.file._
import java.nio.file.attribute.BasicFileAttributes
import scala.collection.Traversable
class TraversePath(path: Path) extends Traversable[(Path, BasicFileAttributes)] {
def foreach(f: ((Path, BasicFileAttributes)) => Unit) {
class Visitor extends SimpleFileVisitor[Path] {
override def visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult = try {
f(file -> attrs)
FileVisitResult.CONTINUE
} catch {
case _ => FileVisitResult.TERMINATE
}
}
Files.walkFileTree(path, new Visitor)
}
}
ProjectHome foreach {
case (file, _) => if (!file.toString.contains(".svn")) println(File)
}
: , Java 7. , .