For a long listing of cases which return a value within a limited domain, how to reduce the otherwise growing number of case declarations ? For instance consider
"abc" match {
case "a" => 1
case "ab" => 1
case "aw" => 2
case "hs" => 2
case "abc" => 1
case _ => 0
}
Tried a Map[Set[String],Int]
where
val matches = Map( Set("a","ab","abc") -> 1, Set("aw","hs") -> 2 )
and defined
def getMatch(key: String, m: Map[Set[String],Int]) = {
val res = m.keys.collectFirst{ case s if s(key) => m(s) }
res.getOrElse(0)
}
Are there simpler and/or more efficient approaches to this ?