Scala – Extractors

  • Post category:Scala
  • Reading time:5 mins read

What is Scala Extractor ?

Scala extractor is an object that has a method called unapply as one of its members. The purpose of unapply method is to match a value and take it apart. The extractor object also defines a dual method apply for building values.

Example

Let us take an example of object defines both apply and unapply methods. The apply method has the same meaning as always: it turns Test into an object that can be applied to arguments in parentheses in the same way a method is applied. So you can write Demo(“proedu”, “gmail.com”) to construct the string “proedu@gmail.com”.

The unapply method is what turns Demo class into an extractor and it reverses the construction process of apply. Where apply takes two strings and forms an email address string out of them, unapply takes an email address and returns potentially two strings: the user and the domain of the address.

The unapply must also handle the case where the given string is not an email address. That’s why unapply returns an Option-type over pairs of strings. Its result is either Some (user, domain) if the string str is an email address with the given user and domain parts, or None, if str is not an email address. Here are some examples as follows.

Syntax

unapply("proedu@gmail.com") equals Some("proedu", "gmail.com")
unapply("proedu") equals None

Following example program shows an extractor object for email addresses.

Example

object Demo {
 def main(args: Array[String]) {
    println ("Apply: " + apply("proedu", "gmail.com"));
    println ("Unapply: " + unapply("proedu@gmail.com"));
    println ("Unapply: " + unapply("proedu"));
 }

 def apply(inputUser: String, domain: String) = {
    inputUser +"@"+ domain
 }

 def unapply(input: String): Option[(String, String)] = {
    val arr = input.split("@")
    if (arr.length == 2){ Some(arr(0), arr(1)) } else { None }
 }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

scalac Demo.scala
scala Demo

Output

Apply: proedu@gmail.com
Unapply: Some((proedu,gmail.com))
Unapply: None

Pattern Matching with Extractors

When an instance of a class is followed by parentheses with a list of zero or more parameters, the compiler invokes the apply method on that instance. We can define apply both in objects and in classes.

As mentioned above, the purpose of the unapply method is to extract a specific value we are looking for. It does the opposite operation apply does. When comparing an extractor object using the match statement the unapply method will be automatically executed.

Example

object Demo {
   def main(args: Array[String]) {
      val x = Demo(10)
      println(x)

      x match {
         case Demo(num) => println(x+" is bigger two times than "+num)
         
         //unapply is invoked
         case _ => println("i cannot calculate")
      }
   }
   def apply(x: Int) = {
      x * 10
   }

   def unapply(z: Int): Option[Int] = {
      if (z % 2 == 0) {
         Some(z / 2)
      }else {
         None
      }
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

scalac Demo.scala
scala Demo

Output

100 is bigger two times than 50