scala 函数中嵌套函数
Scala中的合成功能 (Composition function in Scala)
Scala composition function is a way in which functions are composed in program i.e. mixing of more than one functions to extract some results. In Scala programming language, there are multiple ways to define the composition of a function. They are,
Scala组合函数是一种在程序中组合函数的方法,即混合多个函数以提取一些结果。 在Scala编程语言中,有多种方法来定义函数的组成。 他们是,
Using compose Keyword
使用撰写关键字
Using andthen Keyword
使用然后关键词
Passing method to method
传递方法
1)使用Compose关键字的Scala合成功能 (1) Scala composition function using Compose Keyword )
The compose keyword in Scala is valid for methods that are defined using "val" keyword.
Scala中的compose关键字对使用“ val”关键字定义的方法有效。
Syntax:
句法:
(method1 compose method2)(parameter)
Program:
程序:
object MyObject
{
def main(args: Array[String])
{
println("The percentage is "+(div compose mul)(435))
}
val mul=(a: Int)=> {
a * 100
}
val div=(a: Int) =>{
a / 500
}
}
Output
输出量
The percentage is 87
2)使用andThe关键字的Scala合成函数 (2) Scala composition function using andThen Keyword)
Another composition keyword that works on function defined using val keyword function is andThen.
对使用val关键字function定义的函数起作用的另一个组合关键字是andThen 。
Syntax:
句法:
(method1 andThen method2)(parameter)
Program:
程序:
object myObject
{
def main(args: Array[String])
{
println("The percentage is "+(mul andThen div)(435))
}
val mul=(a: Int)=> {
a * 100
}
val div=(a: Int) =>{
a / 500
}
}
Output
输出量
The percentage is 87
3)Scala合成函数的使用方法 (3) Scala composition function using method to method)
One more way to declaring composition function in Scala is passing a method as a parameter to another method.
在Scala中声明复合函数的另一种方法是将一种方法作为参数传递给另一种方法。
Syntax:
句法:
function1(function2(parameter))
Program:
程序:
object myObject
{
def main(args: Array[String])
{
println("The percentage is "+ ( div(mul(456)) ))
}
val mul=(a: Int)=> {
a * 100
}
val div=(a: Int) =>{
a / 500
}
}
Output
输出量
The percentage is 91
翻译自: https://www.includehelp.com/scala/scala-composition-function.aspx
scala 函数中嵌套函数