8.41. Scala extractor

发布时间 : 2025-10-25 13:35:17 UTC      

Page Views: 9 views

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! 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。