Can this func definition have more graceful syntax?

scala> def classBy(i:Int):String = {
     |   if (i > 4 ) "good"
     |   else if (i > 3) "medium"
     |   else "bad"
     | }
def classBy(i: Int): String

I tried with case statement, which doesn’t work.


When you say “doesn’t work”, what did you try? It can certainly be done, but keep in mind that each language has somewhat different syntax for case statements. (Or in Scala’s case, match expressions.)

Mind, for just three cases I’m not sure I would do it any differently than this…

How about this:

def classBy(i: Int): String = 
  Seq((4, "good"), (3, "medium")).find(i > _._1).fold("bad")(_._2)