8.20. Scala partial application function

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

Page Views: 10 views

Scala partial application function is an expression, you do not need to provide all the parameters needed by the function, only need to provide some, or do not provide the required parameters.

For the following example, we print log information:

import java.util.Date object Test { def main(args: Array[String]) { val date = new Date log(date, "message1" ) Thread.sleep(1000) log(date, "message2" ) Thread.sleep(1000) log(date, "message3" ) } def log(date: Date, message: String) = { println(date + "----" + message) } } 

Execute the above code, and the output is as follows:

$ scalac Test.scala $ scala Test Mon Dec 02 12:52:41 CST 2018----message1 Mon Dec 02 12:52:41 CST 2018----message2 Mon Dec 02 12:52:41 CST 2018----message3 

In the instance log() method receives two parameters: date and message . We called the parameter three times when the program was executed the date values are all the same. message is different.

We can use the partial application function to optimize the above method, binding the first date parameter, use underline for the second parameter(_) replace the missing parameter list and assign the index of the new function value to the variable. The above example is modified as follows:

《地理信息系统原理、技术与方法》  97

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