Simple load balancer model in Scala 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) =&gt; Double): Array[Double] = {
    val array = new Array[Double](size);
    for (el &lt;- 0 to size -1) {
      array(el) = fillingFunction(el);
    }
    return array
  }

  def fillProcessors(size: Integer): Array[Double] = {
    val array = new Array[Double](size);
    for (el &lt;- 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(_ &gt; _)
    var sortedProcessors = processorsArray

    for(processorIndex &lt;- 0 to processorsArray.size -1){
      sortedProcessors(processorIndex) += sortedJobs(processorIndex)
    }

    for(job &lt;- sortedJobs.slice(sortedProcessors.size, sortedJobs.size)){
      sortedProcessors = sortedProcessors.sortWith(_&lt;_)
      sortedProcessors(0) += job
    }
    return result(sortedProcessors)
  }

  def solve(processorsArray: Array[Double], jobsArray: Array[Double],
            solvingFunction: (Array[Double], Array[Double]) =&gt; 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 &lt;- jobs) {
      println(element)
    }
    println("============================")
    println("Results")
    println("Find max solver " + solve(processors, jobs, findMaxSolver))
  }
}
Below are some of the changes I would make to your code. In my opinion one of the nice aspects of Scala is that it gives you all sorts of ways to reduce the amount of work your mind has to do in order to decipher code. As an example, one of the first things I did was declare a type ArrD that is equivalent to Array[Double]. I then just substitued where necessary and the code (to my mind) became more readable. The choice of ArrD was arbitrary on my part, you could if you wanted use ArrayDouble instead. Along these lines I shortened all of you variable and value names.

As the program is set up right now you don't need to pass in a procs array, but I left it in anyway. As you mentioned this is a homework assignment so I'll leave a bit of mystery as to why this is. And really you don't need the take and the drop.

Next note that jobs.sorted is equivalent to jobs.sortWith(_ &gt; _).

And finally (for now) checkout how I initialized jobs and procs. If you still would like to use your randomElements function you should look into the method called tabulate.

Cheers.

object O {

  type ArrD = Array[Double]

  def findMaxSolver(procs: ArrD, jobs: ArrD): Double = {
    val sJobs  = jobs.sorted
    var sProcs = sJobs.take(procs.size)

    for(job &lt;- sJobs.drop(procs.size)){
      sProcs     = sProcs.sorted
      sProcs(0) += job
    }
    sProcs.max
  }

  def solve(procs: ArrD, jobs: ArrD, f: (ArrD, ArrD) =&gt; Double) = f(procs, jobs)

  def run = {
    val jobNum  = 1024
    val procNum = 8
    val jobs    = Array.fill(jobNum)(scala.util.Random.nextDouble())
    val procs   = Array.fill(procNum)(0.0)
    println(s"RESULTS:\nFind max solver ${solve(procs, jobs, findMaxSolver)}")
  }
}