So in one of the places we have this huge variable match case
statement.
Containing almost 150 distinct case
statements
Looks horrible.
I want to break it up into smaller functions, I can group the matches into say 10 pieces and then the number case
statements come down to around 15. Which is fine.
It currently looks like this
massiveCaseVariable match {
case "one" => 1
case "two" => 2
case "three" => 3
case "four" => 4
case "five" => 5
case "six" => 6
}
But I don't want to do it like this
massiveCaseVariable match {
case "one" || "two" || "three" => firstCategory(massiveCaseVariable)
case "four" || "five" || "six" => secondCategory(massiveCaseVariable)
}
def firstCategory(caseVariable: String): Int =
caseVariable match {
case "one" => 1
case "two" => 2
case "three" => 3
}
def secondCategory(caseVariable: String): Int =
caseVariable match {
case "four" => 4
case "five" => 5
case "six" => 6
}
It's too repetitive. Is there a better more concise way to do this?
I'm on Scala 2.11
PS: The examples are for illustration purposes only. I'm definitely not trying to match Strings to Integers