scala 函数中嵌套函数_Scala合成函数

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编程语言中,有多种方法来定义函数的组成。 他们是,

  1. Using compose Keyword

    使用撰写关键字

  2. Using andthen Keyword

    使用然后关键词

  3. 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 函数中嵌套函数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值