Give an integer x, I’d like to return x if it’s within the boundaries of some range. If not, return the value of the boundary it violates.
def keepWithinRange(x:Int, min:Int, max:Int) : Int = {
if (x < min) return min;
if (x > max) return max;
return x;
}
Is there a more elegant way to do this?
I’d write this:
Or this:
Either is more idiomatic than using
returnfor control flow in Scala.