DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
  • How To Use Pandas and Matplotlib To Perform EDA In Python
  • What Is JHipster?
  • 4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment

Trending

  • Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
  • How To Use Pandas and Matplotlib To Perform EDA In Python
  • What Is JHipster?
  • 4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment
  1. DZone
  2. Coding
  3. Languages
  4. Scala at Light Speed, Part 4: Pattern Matching

Scala at Light Speed, Part 4: Pattern Matching

Learn Scala at light speed!

Daniel Ciocirlan user avatar by
Daniel Ciocirlan
CORE ·
Jan. 10, 20 · Tutorial
Like (5)
Save
Tweet
Share
13.41K Views

Join the DZone community and get the full member experience.

Join For Free

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 Essentials,  Part 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

Scala (programming language) Light (web browser)

Opinions expressed by DZone contributors are their own.

Trending

  • Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
  • How To Use Pandas and Matplotlib To Perform EDA In Python
  • What Is JHipster?
  • 4 Expert Tips for High Availability and Disaster Recovery of Your Cloud Deployment

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: