Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!