How to resolve the algorithm Walk a directory/Recursively step by step in the Scala programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Walk a directory/Recursively step by step in the Scala programming language
Table of Contents
Problem Statement
Walk a given directory tree and print files matching a given pattern.
Note: This task is for recursive methods. These tasks should read an entire directory tree, not a single directory.
Note: Please be careful when running any code examples found here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Walk a directory/Recursively step by step in the Scala programming language
Source code in the scala programming language
import java.io.File
object `package` {
def walkTree(file: File): Iterable[File] = {
val children = new Iterable[File] {
def iterator = if (file.isDirectory) file.listFiles.iterator else Iterator.empty
}
Seq(file) ++: children.flatMap(walkTree(_))
}
}
object Test extends App {
val dir = new File("/home/user")
for(f <- walkTree(dir)) println(f)
for(f <- walkTree(dir) if f.getName.endsWith(".mp3")) println(f)
}
You may also check:How to resolve the algorithm Repeat a string step by step in the Tcl programming language
You may also check:How to resolve the algorithm Parallel brute force step by step in the Haskell programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the NS-HUBASIC programming language
You may also check:How to resolve the algorithm S-expressions step by step in the C# programming language
You may also check:How to resolve the algorithm Copy a string step by step in the Objeck programming language