WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Scala Extractor


extractor extracts the parameters that construct the object from the object passed to it.

The Scala standard library contains some predefined extractors, and we'll take a general look at them.

The Scala Extractor is an object with an unapply method. The unapply method is the reverse of the apply method: unapply accepts an object and then extracts the value from the object. The extracted value is usually the value used to construct the object.

The following example demonstrates the extractor object for a mail address:

object Test {
   def main(args: Array[String]) {
      
      println ("Apply method : " + apply("Zara", "gmail.com"));
      println ("Unapply method : " + unapply("Zara@gmail.com"));
      println ("Unapply method : " + unapply("Zara Ali"));

   }
   // Injection method (optional)
   def apply(user: String, domain: String) = {
      user +"@"+ domain
   }

   // Extraction method (required)
   def unapply(str: String): Option[(String, String)] = {
      val parts = str split "@"
      if (parts.length == 2){
         Some(parts(0), parts(1)) 
      }else{
         None
      }
   }
}

Execute the above code, the output is:

$ scalac Test.scala 
$ scala Test
Apply method : Zara@gmail.com
Unapply method : Some((Zara,gmail.com))
Unapply method : None

The above objects define two methods: the apply and unapply methods. With the apply method we can create objects without using the new operation. So you can construct a string "Zara@gmail.com" with the statement Test("Zara", "gmail.com").

The

unapply method is the reverse of the apply method: unapply accepts an object and then extracts the value from the object, and the extracted value is usually the value used to construct the object. In the example we use The Unapply method extracts the suffix of the username and email address from the object.

In the example, the unapply method returns None when the incoming string is not an email address. The code is demonstrated as follows:

unapply("Zara@ Gmail.com") equivalent to Some("Zara" , "gmail.com" )
Unapply("Zara Ali") equal to None

Extractor uses pattern matching

When we instantiate a class, we can take 0 or more parameters, and the compiler will call the apply method when instantiating. We can define the apply method in both classes and objects.

As we mentioned before, unapply is used to extract the value we specify for the lookup, which is the opposite of apply. When we use the match statement in the extractor object, unapply will be executed automatically as follows:

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

      x match
      {
         case Test(num) => println(x + " Yes " + num + " Twice!")
         //unapply Called
         case _ => println("Unable to calculate")
      }

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

Execute the above code and output:

< Span class="pln">$ scalac Test.scala
$ scala Test
10
10 Yes 5  twice!