scala闭包 变量_Scala闭包,自由和绑定变量,匿名函数

scala闭包 变量

A closure can be defined as the function whose return value depends on the value of one or more variable defined outside this function. A closure is an instance of a function, a value whose non-local variables have been bound either to values or to storage locations.

闭包可以定义为函数的返回值取决于该函数外部定义的一个或多个变量的值。 闭包是函数的实例,该函数的值的非局部变量已绑定到值或存储位置。

Consider an example as below;

考虑如下示例:

object Divide {

def main(args:Array[String]) {
println( "Divided value = " + divider(4) )
println( "Divided value = " + divider(6) )
println( "Divided value = " + divider(8) )
println( "Divided value = " + divider(10) )
}

var div = 2
val divider = (i:Int) => i/div
}

We are defining an object Divide with a main function that accepts arguments of String Array type and printing the value of the divider by calling a variable divider defined outside the main method where we divide the value passed to divider with the div value “2” initialized outside the main method.

我们使用主函数定义一个对象Divide,该函数接受String Array类型的参数,并通过调用main方法外部定义的变量除法器来打印除法器的值,在该方法中,我们将传递给除法器的值除以div值“ 2”初始化以外的主要方法。

i and div are the two free variables. i is the formal parameter to the function. div is not a formal parameter and has a reference to a variable outside the function but in the enclosing scope as

i和div是两个自由变量。 i是函数的形式参数。 div不是形式参数,并且在函数外部但在封闭范围内具有对变量的引用

var div = 2
val divider = (i:Int) => i/div

Run the above example by typing Divide.main(null)

通过键入Divide.main(null)运行以上示例

Output:

输出:

Divided value = 2
Divided value = 3
Divided value = 4
Divided value = 5

Below image shows the above programs execution in Scala shell.

下图显示了以上程序在Scala shell中的执行。

自由变量和绑定变量 (Free variables and bound variables)

The variables that are used in function but are neither local variables nor formal parameters to the function are called free variables.

在函数中使用但既不是局部变量也不是函数的形式参数的变量称为自由变量。

Consider an example for free variable;

考虑一个自由变量的例子。

class Marks(m1: Int) {

var marks : Int = m1

def totalmarks(ma1:Int) {
marks = marks+10
println("Total marks secured: " +marks)
}
}

object m1 {

def main(args:Array[String]) {
var m1 = new Marks(45)
m1.totalmarks(45)
}
}

Run by typing m1.main(null) and you will get output Total marks secured: 55.

输入m1.main(null)运行,您将获得Total marks secured: 55输出Total marks secured: 55

A bound variable is a variable that was previously free, but has been bound to a specific value or set of values.

绑定变量是以前没有使用但已绑定到特定值或一组值的变量。

Consider an example for bound variable;

考虑一个绑定变量的例子。

val result = (i:Int) => i * 6

Run by typing result(32) and you will get output like res2: Int = 192.

输入result(32)运行,您将得到类似于res2: Int = 192输出res2: Int = 192

Here i is bound to a new value each time the result is called.

每次调用结果时,i都会绑定到一个新值。

匿名功能 (Anonymous functions)

An anonymous function is a function definition that is not bound to an identifier. In other words, the function definition doesn’t have an implicit name.

匿名函数是未绑定到标识符的函数定义。 换句话说,函数定义没有隐式名称。

Consider an example for anonymous function as below;

考虑一个匿名函数的例子,如下:

var a = (x: Int) => x + 1

As you can notice we have not specified any identifier for the function after the equal symbol. There is no binding associated with the function definition. This the reason why it is called as an Anonymous function. After the specification of the statement the interpreter gives an output as;

如您所见,我们在等号后没有为函数指定任何标识符。 没有与函数定义关联的绑定。 这就是为什么将其称为匿名函数的原因。 在声明的说明之后,解释器给出的输出为:

a: Int => Int = <function1>

a has the object reference to this anonymous function and it can be called as a(4) that will produce output similar as res16: Int = 5. Below image shows execution of bound variable and anonymous function example in Scala shell.

a具有对该匿名函数的对象引用,可以将其称为a(4) ,其产生的输出类似于res16: Int = 5 。 下图显示了Scala shell中绑定变量和匿名函数示例的执行。

That’s all for Closures and anonymous functions in Scala programming, we will look into more Scala core features in coming posts.

这就是Scala编程中的Closures和匿名函数的全部内容,我们将在以后的文章中探讨更多Scala核心功能。

翻译自: https://www.journaldev.com/7751/scala-closures-free-and-bound-variables-anonymous-functions

scala闭包 变量

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值