I am fairly new to Scala, and would like to know if it is possible for a match to execute multiple matching cases at once. Without getting into too much detail, I am basically working on a function that "scores" a certain piece of text according to various traits; these traits can overlap, and multiple traits can be true for one given string.
To illustrate what I want in code, it would look something like this:
Say we have a String, str, with a value of "Hello World". I would like something along the lines of the following:
str match {
case i if !i.isEmpty => 2
case i if i.startsWith("world") => 5
case i if i.contains("world") => 3
case _ => 0
}
I would like the above code to trigger both the first and third conditions, effectively returning both 2 and 3 (as a tuple or in any other way).
Is this possible?
Edit: I know this can be done with a chain of if's, which is the approach I took. I'm just curious if something like the above implementation is possible.
List[(String => Boolean, Int)]and then usepredicateList.collect {case (p, i) if p(myWordToTest) => i}((if (!i.isEmpty) 2) :: (if (i.startsWith("world")) 5) :: (if (i.contains("world")) 3) :: Nil) map {case i: Int => i case _ => 0} sumbut wanted to see if there was a more elegant way. @Marth I'd have expected the return types to possibly be aTuple, if something like that was possible.