Forming two vectors with intervals and code values for these intervals may help: ( throws NoSuchElementException if the value supplied in not in any of the intervals provided.
).I personally favour this because it will be flexible to add more data ( both intervals and code values for these intervals in stead of adding long list of case statements in code for all the intervals if the value supplied is to be tested against large number of intervals and corresponding code values for these intervals. It will be flexible enough to variable intervals in stead of constant ranges. )
val v1 = Vector((0,10),(10,20),(20,30)); val v2 = Vector(0,1,2)
def getCode(x:Int,ivals:Vector[(Int,Int)],codes:Vector[Int]) ={
v1.zip(v2).find(t=>t._1._1<x && t._1._2>=x).get._2 }
Usage:
scala> val v1 = Vector((0,10),(10,20),(20,30))
v1: scala.collection.immutable.Vector[(Int, Int)] = Vector((0,10), (10,20), (20,30))
scala> val v2 = Vector(0,1,2)
v2: scala.collection.immutable.Vector[Int] = Vector(0, 1, 2)
scala> getCode(5,v1,v2)
res14: Int = 0
scala> getCode(23,v1,v2)
res15: Int = 2