concept pattern matching in category scala

appears as: pattern matching
Get Programming with Scala

This is an excerpt from Manning's book Get Programming with Scala.

In a similar way to the apply method, unapply is a static function that lives in the companion object of a class: it takes an instance class, and it returns its decomposed representation. For example, you could identify a Person by providing a pair with its name and age. The unapply function allows you to use pattern matching, a powerful and expressive tool of the Scala toolbox to match conditions (e.g., age bigger than 18) based on the decomposition of a class: you are going to learn about this in the next unit.

After discovering how to express interfaces in Scala using a trait, I’ll now introduce you to pattern matching. You can use a pattern matching statement as an alternative to an if…else one, particularly useful when you need to consider have many different condition branches. A pattern matching statement is the equivalent of the switch/case statement of other languages, such as Java, JavaScript, and C++. Pattern matching is more powerful and versatile than a classic switch/case statement thanks to its powerful syntax and dedicated support for sealed element and classes. In this lesson, you’ll learn how pattern matching compares to an if…else statement, and its base uses. You’ll also discover to define a pattern matching statement over a sealed set of values and how the compiler can warn you if you forget to consider any of them. Pattern matching has also dedicated support for classes, but I’ll introduce you to this topic later on when we are going to discuss case classes. In the capstone, you’ll use pattern matching to map country names to their timezones.

Although this program works as expected, it is not particularly easy to read because of the long list of cases to consider. Also, the condition has always the same structure: “is n equal to some number?”. You are trying to express a one-to-one mapping between numbers and days of the week: pattern matching is a good fit for some refactoring that makes your code more readable and concise. Listing 14.2 shows you how to rewrite the function using pattern matching:

Listing 14.2: Mapping numbers with days of the week
def dayOfWeek(n: Int): String = n match {
  case 1 => "Sunday"
  case 2 => "Monday"
  case 3 => "Tuesday"
  case 4 => "Wednesday"
  case 5 => "Thursday"
  case 6 => "Friday"
  case 7 => "Saturday"
  case _ => "Unknown" //#A
}

You can visualize pattern matching as a statement with a collection of cases: you can identify it by an expression with a match keyword and one or more case keyword statements. It considers each case in order of declaration: once it can successfully match a case, it continues the computation by evaluating the corresponding expression.

At a first look, pattern matching seems similar to the switch/case statement of other languages like Java or C++, but it is a lot more powerful thanks to expressive syntax and its support for sealed types, which we’ll discuss in the next section, and case classes, which you’ll see in lesson 19. Let me demonstrate how powerful pattern matching can be with a trivial example.