jeudi 21 octobre 2021

scala two variables if else condition

How can I do this better in scala?

def fooOutput(v1: Double, v2: Int): Int = {
    if (v1 >= 9 & v2 >= 2) {
      5
    }

    else if (v1 >= 8) {
      4
    }
    else if (v1 >= 4) {
      3
    }
    else {
      2
    }
  }

I do not like this if else if else logic. can i use switch statement for two variables or maybe some better scala functional approach?

Aucun commentaire:

Enregistrer un commentaire