Search code examples
scalafunctional-programmingdistributed-computing

Replacement of if with higher order method


using if block in scala for distributed computing is least recommended. I have code and i want to replace if with Scala higher order method. How can i do that. Detail code is given Here

Some part of code that contains if block is.

var bat = DenseVector.fill(N)(new BAT12(d , MinVal , MaxVal ))
bat.foreach{x => x.BestPosition = x.position;x.fitness =  Sphere(x.position)  ; x.BestFitness = x.fitness}
bat.foreach(x =>
if(x.BestFitness < GlobalBest_Fitness)
{
 GlobalBest_Fitness =x.BestFitness ;GlobalBest_Position = x.BestPosition
})

Solution

  • Try

    bat.filter(_.BestFitness < GlobalBest_Fitness).foreach { x =>
      GlobalBest_Fitness = x.BestFitness
      GlobalBest_Position = x.BestPosition
    }