return, . - x;, . , if/else if/else:
def limit(x : Double, min: Double, max : Double) =
if (x < min) min
else if (x > max) max
else x
if/else if/else , .
You can also use pattern matching (this is also one expression):
def limit(x : Double, min: Double, max : Double) = x match {
case x if x < min => min
case x if x > max => max
case _ => x
}
I do not think that your second example can be called a "good scala". In such a simple case, it just complicates all this and has three return points (instead of a single return point). It also adds more unnecessary template.
source
share