Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda