Intro to Pattern Matching

Question Click to View Answer

What does the following code print?

def dayOfWeek(x: Int) = x match {
  case 1 => "Mon"
  case 2 => "Tue"
  case 3 => "Wed"
  case _ => "Unknown"
}
println(dayOfWeek(2))
println(dayOfWeek(-1))

dayOfWeek(2) returns "Tue".

dayOfWeek(-1) returns "Unknown".

This example is pattern matching on different integer values.

What does the following code print?

def indexOfDay(d: String) = d match {
  case "Mon" => 1
  case "Tue" => 2
  case "Wed" => 3
  case _ => -1
}
println(indexOfDay("Wed"))
println(indexOfDay("Whatever"))

indexOfDay("Wed") returns 1.

indexOfDay("Whatever") returns -1.

This example shows how to pattern match different string values.

What does the following code print?

Return "Fizz" if the number is divisible by 3, "Buzz" if the number is divisible by 5, and "FizzBuzz" if the number is divisible by both 3 and 5.

def fizzBuzz(num: Int) = (num % 3, num % 5) match {
  case (0, 0) => "FizzBuzz"
  case (0, _) => "Fizz"
  case (_, 0) => "Buzz"
  case _ => ""
}
println(fizzBuzz(3))
println(fizzBuzz(15))

fizzBuzz(3) returns "Fizz".

fizzBuzz(15) returns "FizzBuzz".

This example shows how to pattern match tuples.

What does the following code print?

case class Point(x: Int, y: Int)
def direction(p: Point) = p match {
  case Point(0, 0) => "origin"
  case Point(_, 0) => "horizontal"
  case Point(0, _) => "vertical"
  case _ => "diagonal"
}
println(direction(Point(10, 0)))
println(direction(Point(0, 5)))
println(direction(Point(2, 2)))

direction(Point(10, 0)) returns "horizontal".

direction(Point(0, 5)) returns "vertical".

direction(Point(2, 2)) returns "diagonal".

This example shows how to pattern match on case class objects with different constructor values.

What does the following code print?

def americanDate(s: String) = s match {
  case s"$day-$month-$year" => s"$month/$day/$year"
  case _ => "not a date"
}
println(americanDate("22-1-1965"))
println(americanDate("10-1988"))

americanDate("22-1-1965") returns "1/22/1965".

americanDate("10-1988") returns "not a date".

This example shows how to pattern match on string patterns.

What does the following code print?

case class Person(name: String, title: String)
def greet(p: Person) = p match {
  case Person(s"$firstName $lastName", title) => s"Hello $title $lastName"
  case Person(name, title) => s"Hello $title $name"
}
println(greet(Person("Ron", "Master")))
println(greet(Person("Matthew Powers", "Mr")))

greet(Person("Ron", "Master")) returns "Hello Master Ron".

greet(Person("Matthew Powers", "Mr")) returns "Hello Mr Powers".

This example demonstrates how to perform nested pattern matching.

What does the following code print?

val a = Array[(Int, String)]((1, "one"), (2, "two"), (3, "three"))
val res = for ((i, s) <- a) yield(s + i)
println(res) 

Array("one1", "two2", "three3") is printed.

This example shows how to pattern match in a for loop.

Scala makes it easy to iterate over a collection of tuples.

What does the following code print?

case class Coffee(origin: String, roast: String)
val lomaverde = Coffee("Colombia", "medium")
val Coffee(a, b) = lomaverde
println(a)
println(b)

println(a) prints "Colombia".

println(b) prints "medium".

This example shows how to pattern match in val statements.

What does the following code print?

def typeFinder(something: Any) = something match {
  case i: Int => "I am an integer"
  case s: String => "I am a string"
  case _ => "Not sure what type I am"
}
println(typeFinder(56))
println(typeFinder("hello"))

typeFinder(56) returns "I am an integer".

typeFinder("hello") returns "I am a string".

This example shows how pattern matching makes it easy to handle various types of input data.

What does the following code print?

case class Player(name: String, score: Int)

def message(player: Player) = player match {
  case Player(_, score) if score > 100000 => "Get a job"
  case Player(name, _) => "Hey " + name + ", nice to see you again!"
}
message(Player("bob", 10))
message(Player("carolina", 500000))

message(Player("bob", 10)) returns "Hey bob, nice to see you again!".

message(Player("carolina", 500000)) returns "Get a job".

This example shows how to pattern match on objects with different values.