8.41. Scala extractor

发布时间 :2023-11-21 23:00:03 UTC      

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:

8.41.1. 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:

8.41.2. 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!

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.