Scala extractor


Release date:2023-11-22 Update date:2023-11-22 Editor:admin View counts:163

Label:

Scala extractor

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

The Scala standard library contains some predefined extractors, which we will take a quick look at.

The Scala extractor is an object with a unapply method. The unapply method can be regarded as apply . The reverse operation of the unapply accept an object and then extract the value from the object, which is usually used to construct the value of the object.

The following example demonstrates the extractor object for an email address:

Example

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, and the output is as follows:

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

The above object defines two methods: apply and unapply method. Pass through apply method we do not need to use new . You can createan object by doing so. So you can construct a string “Zara@gmail.com” by saying Test (“Zara”, “gmail.com”).

The unapply method can be regarded as the apply reverse operation of the unapply accept an object and then extract the value from the object, which is usually used to construct the value of the object. In the example, we use Unapply method to extract the suffix of the user name and e-mail address from the object.

In the instance unapply method is returned when the passed string is not an email address None . The code is demonstrated as follows:

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

Extractor uses pattern matching

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

As we mentioned before. unapply used to extract the value we specified to find, which is similar to that of the apply operation is the opposite. When we use the match statement is unapply , which will be executed automatically, as follows:

Example

object Test {
   def main(args: Array[String]) {

      val x = Test(5)
      println(x)
      x match
      {
         case Test(num) => println(X+"is"+num+" twice!")
         //unapply Called
         case \_ => println("incalculable")
      }
   }
   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 the output is as follows:

$ scalac Test.scala
$ scala Test
10
10 is twice as much as 5!

Powered by TorCMS (https://github.com/bukun/TorCMS).