C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Case: We use the case-keyword to specify cases to return. Here we use simple cases for single Ints, but more complex forms may be used.
Scala program that uses match
// Use match on parameter id and return a String.
// ... Default case returns "None" String.
def colorCode(id: Int): String = id match {
case 0 => "Blue"
case 1 => "Red"
case 2 => "Green"
case _ => "None"
}
// Get color code for this argument.
val result = colorCode(0)
println(result)
val result2 = colorCode(1)
println(result2)
val result3 = colorCode(4000)
println(result3)
Output
Blue
Red
None
Here: The testList function matches lists with two or more elements, and returns true if the second element is greater than the first.
Tip: The identifiers in the list pattern do not matter, but only the first two are elements we can compare.
Note: The third identifier is the remainder of the list. So here "a" is element 1, "b" is element 2 and "c" is all remaining elements.
Scala program that uses match, list pattern
// Return true if list has two or more elements.
// Second element must be greater than the first.
// ... Return false for all other lists.
def testList(x: List[Int]): Boolean = x match {
case a :: b :: c => b > a
case _ => false
}
// Call testList.
val result1 = testList(List(10, 20, 30))
val result2 = testList(List(20, 10))
val result3 = testList(List())
val result4 = testList(List(1, 0, 2, 3))
val result5 = testList(List(0, 1, 2, 3))
println(result1)
println(result2)
println(result3)
println(result4)
println(result5)
Output
true
false
false
false
true
Here: GetOption returns an Option that wraps an Int. If we pass 1 or greater, we get Some value. Otherwise we get None.
Result: We pass 2 to getOption which returns a valid option. The Some case in the match block is reached. A value exists.
Scala program that uses Some, None option match
// Return Some or None option based on integer.
def getOption(n: Int): Option[Int] = if (n >= 1) Some(n)
else None
// Get an option.
val result = getOption(2)
// Match option on Some and None.
result match {
case Some(x) => println("Value exists")
case None => println("Nothing exists")
}
Output
Value exists
Here: Our match tests the "magnitude" value. It handles the case of greater than or equal to 4, and the case of less than or equal to 1.
String: This match construct returns a string. Its result is stored in the "result" value and then printed to the screen.
printlnScala program that uses match with if-expressions
val magnitude = 5
// Match on magnitude, returning a string.
// ... Handle greater than or equal to 4.
// Handle less than or equal to 1.
val result = magnitude match {
case m if m >= 4 => "Big, greater than or equal to 4"
case m if m <= 1 => "Small, less than or equal to 1"
case _ => "Medium"
}
// Print result.
println(result)
Output
Big, greater than or equal to 4
List pattern matching:
The pattern x :: y :: xs matches lists of length >= 2,
binding x to the list's first element, y to the list's second element,
and xs to the remainder.