For one of my cases in algorithms I am required to create a model of a load balancer model for multiple processors. Lately I became interested in Scala so I decided that it would be great to create this program in Scala. I'm not really familiar with the concept of functional programming so any feedback will be welcome. Please note that I am not asking about the correctness of my algorithm as this is my homework.
object HelloWorld {
def randomElements(arg: Integer): Double = {
val r = scala.util.Random
return r.nextDouble()
}
// used for debugging
def return1Elements(arg: Integer): Double ={
return 1
}
def fillJobs(size: Integer, fillingFunction: (Integer) => Double): Array[Double] = {
val array = new Array[Double](size);
for (el <- 0 to size -1) {
array(el) = fillingFunction(el);
}
return array
}
def fillProcessors(size: Integer): Array[Double] = {
val array = new Array[Double](size);
for (el <- 0 to size -1) {
array(el) = 0
}
return array
}
def result(processors: Array[Double]): Double = {
return processors.max
}
def findMaxSolver(processorsArray: Array[Double], jobsArray: Array[Double]): Double = {
val sortedJobs = jobsArray.sortWith(_ > _)
var sortedProcessors = processorsArray
for(processorIndex <- 0 to processorsArray.size -1){
sortedProcessors(processorIndex) += sortedJobs(processorIndex)
}
for(job <- sortedJobs.slice(sortedProcessors.size, sortedJobs.size)){
sortedProcessors = sortedProcessors.sortWith(_<_)
sortedProcessors(0) += job
}
return result(sortedProcessors)
}
def solve(processorsArray: Array[Double], jobsArray: Array[Double],
solvingFunction: (Array[Double], Array[Double]) => Double): Double = {
val processors = processorsArray.clone()
val jobs = jobsArray.clone()
return solvingFunction(processors, jobs)
}
def main(args: Array[String]) {
val JOB_NUMBER = 1024
val PROCESSOR_NUMBER = 8
val jobs = fillJobs(JOB_NUMBER, randomElements)
val processors = fillProcessors(PROCESSOR_NUMBER)
println("Job times")
println("============================")
for (element <- jobs) {
println(element)
}
println("============================")
println("Results")
println("Find max solver " + solve(processors, jobs, findMaxSolver))
}
}