Pattern Matching: The Most Powerful Tool of Functional Programming

Reading Time: 2 minutes

Hi, community, in this blog, we will be covering what is pattern matching, when should we use it, and at the end, we’ll compare it with if-else.

What is Pattern Matching?

Pattern Matching is one of the most powerful tools in Scala. It’s similar to a switch statement in java but with loads of functionalities in comparison to a switch statement.

Definition

Pattern Matching is basically a sequence of case statements where each case statement defines a pattern and expression which is invoked whenever there’s a match.

Let’s take an example:

package com.knoldus.patternMatching

object patternMatching extends App{
  def typeMatching(x:Any)= {
    x match {
      case i:Integer => "It's an integer: " + i
      case s:String => "It's a string: " + s
      case b:Boolean => "Its a boolean: " + b
      case _ => "Something Else"
    }
  }
  println(typeMatching(45.569))
  println(typeMatching("Knoldus"))
  println(typeMatching(false))
  println(typeMatching(21))
}

The output of the program:

Something Else
It's a string: Knoldus
Its a boolean: false
It's an integer: 21

Explanation :

We are comparing one object to another where each case statement consists of a pattern in our case integer, string, boolean, and _ (a wildcard which is used as a default case) and expression separated by “=>” arrow symbol from the pattern.

When Should we Use Pattern Matching?

Object-Oriented Programming has taught us to encapsulate everything into an object, so we encapsulate both methods and data structures.

When encapsulated we ignore data structures and for every new functionality, we add a new method.

The main issue arises when we are required to include an excess of using similar data construction and we as a whole know each usefulness is directly related to the new method.

So to solve this problem, we follow a functional approach.

Pattern Matching helps you deconstruct data structures into their constituent parts.

Let’s take an example

package com.knoldus.patternMatching

object Deconstruct extends App{
  case class Employee(name:String, engineerLevel:Int)
  val person = Employee("Patrick", 3)
  val secondPerson = Employee("Izzie", 2)

  val employeeDetail = person match {
    case Employee(n, l) => s"Hi my name is $n and I am a level $l engineer"
    case _ => "May be you are an Intern"
  }
  println(employeeDetail)

}

Output

Hi my name is Patrick and I am a level 3 engineer

Which is better Pattern Maching or if-else?

Well, pattern matching is indeed far more powerful than if-else because of the loads of functionalities it has, and also Scala developers are advised to prefer pattern match over if-else.

So does that mean we should completely discard if-else?

Well, not quite if you are working with boolean it’s always advisable to use if-else since boolean doesn’t convey any information at the type level.

If we use pattern matching the compiler generates loads of code under the hood which results in more memory consumption and it takes more time to compile as compared to if-else.

Conclusion

Much obliged to you all for coming to the furthest limit of this blog, presently you have perused the blog so more likely than not acquired not entire but rather slight information on Scala Pattern Matching.

If you want to get more knowledge of pattern matching you can refer to the below link.

Refrences

https://docs.scala-lang.org/tour/pattern-matching.html

Written by 

Hi community, I am Raviyanshu from Dehradun a tech enthusiastic trying to make something useful with the help of 0 & 1.

1 thought on “Pattern Matching: The Most Powerful Tool of Functional Programming3 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading