I'm learning Scala and could use another set of eyes on the code below. I'm creating a test runner for the 99 scala problems set. I figure between actually solving the problems and working through the pains of setting up a new IDE (trying IntelliJ from Eclipse), using new build tools (trying SBT from Maven), new testing tools etc, I'll have the language down pat. The code below is a runner that grabs all the solution files in a given directory and executes them. While the code works, I can't help but feel as though I'm doing this in the 'Java' way and not the 'Scala' way (ie - I'm not being very 'functional' in my implementation). Does anyone who knows Scala pretty well have any comment on what I've written thus far?
runner:
import java.io.File
import org.apache.commons.io.FilenameUtils
/**
* boodstraps program
*/
object bootStrap {
def main(args: Array[String]): Unit = {
// go through solution files and execute
for (fName <- getSolutionFiles(new File("./src/main/scala"))) {
val fNameNoExtS = FilenameUtils.removeExtension(fName.getName)
solutionRunner(fNameNoExtS)
}
}
def solutionRunner(name: String): Unit = {
println(name)
val classLoader = this.getClass.getClassLoader
val clazz = classLoader.loadClass(name)
val solutionMethod = clazz.getDeclaredMethod("solution")
solutionMethod.invoke(clazz.newInstance)
}
/**
* trolls through working directory and all sub directories in search of files
* with names starting with "sp_" ('sp' for 'scala problem'). thanks to
* stack overflow for base code: http://stackoverflow.com/questions/2637643/
* how-do-i-list-all-files-in-a-subdirectory-in-scala
*
* @param f - java File object
* @return targetFileList - Array[java.io.File] - list of file objects
* that have names starting with "sp_"
*/
def getSolutionFiles(f: File): Array[File] = {
// create lists of files, directories, and target files
val fullFileList = f.listFiles
val targetFileList = fullFileList.filter(_.getName.matches("^sp_.*"))
val dirList = fullFileList.filter(_.isDirectory)
// recursively append target files located in sub directories to list.
// return populated list to caller.
targetFileList ++ dirList.flatMap(getSolutionFiles)
}
}
simple 'sp' file:
class sp_01 {
def solution(): Unit = {
println("in sp_01, baby!")
}
}