scala部分应用函数_Scala中的部分应用函数

scala部分应用函数

部分应用的功能 (Partially applied functions)

Partially applied functions, are actually those function that allows you to implement function calls with partial arguments i.e. using only a few values in a function call and uses rest values form a common call initiation.

部分应用的函数实际上是那些允许您使用部分参数来实现函数调用的函数,即,在函数调用中仅使用几个值,而使用剩余值形成一个公共调用启动。

It's a bit trick concept, so to understand it lets take an example. Suppose you define need to find the percentage of all students. You define a general function that takes two arguments total marks and marks obtained and find the percentage. And for a class, the total marks are the same. The classical way is to pass to full arguments to each call of the function. But using the partially applied function, you can place one argument as a pre-defined argument that will use its values as arguments in the function call. As in our example, initialization of percentage method named ninth is made with an initial value of total marks argument defining it as a partially applied function.

这是一个技巧,所以要理解它,可以举个例子。 假设您定义需要查找所有学生的百分比。 您定义了一个通用函数,该函数接受两个参数,总共为标记,而获得的标记为百分比。 对于一个班级,总成绩是相同的。 经典方法是将完整参数传递给函数的每次调用。 但是,使用部分应用的函数,您可以将一个参数作为预定义的参数放置,并将其值用作函数调用中的参数。 就像在我们的示例中一样,使用总分数参数的初始值对名为ninth的百分比方法进行初始化,将其定义为部分应用的函数

Syntax:

句法:

    val pAfName = funcName(arg1Val, _ : datatype);
    pAfName(arg2Val)

Explanation:

说明:

This syntax is for the initializing pAfName variable for function funcName that defines partial argument feed of arg1Val. This value can be used for later is code when the only arg2Val is used to call the function.

该语法用于初始化函数funcName的pAfName变量,该变量定义了arg1Val的部分自变量。 当仅使用arg2Val调用该函数时,此值可用于以后的代码。

Example:

例:

object myClass {
    def percentage(totalMarks: Int, marksObtained: Int) = {
      println("Percentage Obtianed :");
      println( ((marksObtained*100)/totalMarks) + " %")
   }
   
   def main(args: Array[String]) {
      val  ninth = percentage(350 , _ : Int)
      println("Student 1")
      ninth( 245 )
      println("Student 2")
      ninth( 325 )
      println("Student 3")
      ninth( 102 )
   }   
}

Output

输出量

Student 1
Percentage Obtianed :
70 %
Student 2
Percentage Obtianed :
92 %
Student 3
Percentage Obtianed :
29 %

Code explanation:

代码说明:

The above code is based on the example that we have discussed in this tutorial. We have made a function percentage that prints the percent of the student. This function takes two arguments totalMarks and marksObtained. But for a set of function, the totalMarks will be the same. In this case, we have defined a function named ninth that has value 350. And this ninth function is used three times with different arguments that print the calculated result.

上面的代码基于我们在本教程中讨论的示例。 我们制作了一个函数百分比 ,用于打印学生的百分比。 此函数采用两个参数totalMarks和marksObtained 。 但是对于一组功能, totalMarks将相同。 在这种情况下,我们定义了一个名为ninth的函数,该函数的值为350。并且该ninth函数使用了三次,并带有不同的参数,这些参数将打印计算的结果。

翻译自: https://www.includehelp.com/scala/partially-applied-functions-in-scala.aspx

scala部分应用函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值