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?
You can move your condition into pattern guards, not sure that it will be much better though:
def fooOutput(v1: Double, v2: Int): Int = v1 match {
case _ if v1 >= 9 && v2 >= 2 => 5
case _ if v1 >= 8 => 4
case _ if v1 >= 4 => 3
case _ => 2
}