{{announcement.body}}
{{announcement.title}}

Scala at Light Speed, Part 4: Pattern Matching

DZone 's Guide to

Scala at Light Speed, Part 4: Pattern Matching

Learn Scala at light speed!

· Java Zone ·
Free Resource

Learn Scala at light speed!

This article series is for busy programmers who want to learn Scala fast, in 2 hours or less. The articles are the written version of Rock the JVM's Scala at Light Speed mini-course, which you can find for free on YouTube or on the Rock the JVM website in video form.

This is the third article of the series, which will focus on Scala as a functional programming language. You can watch it in video form here or in the embedded video below.

So far, we've covered:

  • How to get started with Scala
  • The very basics: values, expressions, types
  • Object orientation: classes, instances, singletons, methods and basic generics
  • Functional programming
You may also like:  Scala at the Speed of Light, Part 1: The EssentialsPart 2: Object Orientation, and Part 3: Functional Programming

Enter Pattern Matching

Pattern matching is one of Scala's most powerful features. We'll start by analogy to other programming languages, where you've likely seen a switch statement. A pattern match is like a switch on steroids:

Scala




xxxxxxxxxx
1


 
1
  val anInteger = 55
2
  val order = anInteger match {
3
    case 1 => "first"
4
    case 2 => "second"
5
    case 3 => "third"
6
    case _ => anInteger + "th"
7
  }



Much like anything else in Scala, a pattern matching structure is an expression, because it reduces to a value (one of the case values on the right-hand side of  =>). As a side note, the compiler is able to infer the type of the expression by inspecting the types returned by all cases (in this case String).

A few things about pattern matching:

  • The underscore stands for anything (wildcard)
  • The patterns are tested in order; the first one that matches will dictate the value of the expression.
  • If no patterns match, a MatchError will be thrown.

More Than Just a Switch

Pattern matching can not only test for values, but for the structure of data, and can bind pieces of that data to names which can be used in the returned expression. The classic example is with case classes (which we mentioned in Part 2 are easily decomposable with pattern matching).

Scala




xxxxxxxxxx
1


 
1
  // Case class decomposition
2
  case class Person(name: String, age: Int)
3
  val bob = Person("Bob", 43) // Person.apply("Bob", 43)
4
 
          
5
  val personGreeting = bob match {
6
    case Person(n, a) => s"Hi, my name is $n and I am $a years old."
7
    case _ => "Something else"
8
  }



In this PM expression, we're not testing bob against a definitive value, but we're matching it against the Person structure, and in the case it does match, we're binding its fields to the names n and a so we can use them in the returned expression (notice the s-interpolated string).

PMs can match case classes and this kind of structures at a deep level, so if a case class contains a member that's another case class, you can match nested case classes with no problems.

Decomposing Other Structures

Some of the often used examples of PM decomposition include deconstructing tuples and lists. Let's see some code:

Scala




xxxxxxxxxx
1


 
1
  // deconstructing tuples
2
  val aTuple = ("Bon Jovi", "Rock")
3
  val bandDescription = aTuple match {
4
    case (band, genre) => s"$band belongs to the genre $genre"
5
    case _ => "I don't know what you're talking about"
6
  }



Much like case classes, we're matching against the tuple structure, and in the case, it matches; we're binding the members of the tuple to the respective names. 

The truly magical feature is decomposing other structures, like lists:

Scala




xxxxxxxxxx
1


 
1
  // decomposing lists
2
  val aList = List(1,2,3)
3
  val listDescription = aList match {
4
    case List(first, 2, _) => s"List with 2 on its second position, starting with $first"
5
    case _ => "unknown list"
6
  }



The PM case says: try to match aList to the structure of a list containing exactly 3 elements; the first can be anything and will be named first, the second must be exactly 2, and the third is unimportant.

There are lots of other patterns and capabilities to the PM feature in Scala, but we will keep this short for now so that we don't needlessly overcomplicate things.

In the next article in the series, we will go through some of the advanced Scala features that you will likely see in real-life code. Stay tuned!

Further Reading


Clean Code Best Practices in Scala

A Journey With Scala

[DZone Refcard] Getting Started With Scala

Topics:
scala ,functional programming ,pattern matching

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}