用于定义Scala中的Function的def或val [英] def or val for defining Function in Scala

查看:100
本文介绍了用于定义Scala中的Function的def或val的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习大学的Programming Paradigms,并阅读由演讲者提供的课程材料,它以这种方式定义了一个函数:

  val double =(x:Int)=> 2 * x 
double:Int => Int =< function1>

但是从我自己的研究中,我发现并习惯于像这样定义相同的函数:

  def d(x:Int)= 2 * x 
d:(x:Int)Int

我是Scala的新手。这两个定义都给出了结果:

  res21:Int = 8 

传递 4 作为参数。
现在我的主要问题是为什么讲师倾向于使用 val 来定义函数?除非使用 val 给出了一些我不知道的优点,除了我理解使用 val 在程序后面使某个名称成为占位符,我可能会错误地写 val double = 5 和功能将会消失!
在这个阶段,我确信我学会了定义一个函数的更好方法,除非有人会告诉我。否则。

解决方案

严格地说, def d(x:Int)= 2 * x 是一种方法,不是函数,但是scala可以透明地将(提升) 。这意味着你可以在任何需要 Int =>的地方使用 d Int Function。



执行此转换的开销很小,因为每次都会创建一个新的Function实例。我们可以在这里看到这种情况:

  val double =(x:Int)=> 2 * x 
def d(x:Int)= 2 * x

def printFunc(f:Int => Int)= println(f.hashCode())

printFunc(double)
printFunc(double)
printFunc(d)
printFunc(d)

其结果如下所示:

  1477986427 
1477986427
574533740
1102091268

使用<$ c显式定义函数时可以看到$ c> val ,我们的程序只创建一个Function并在我们作为参数传递给 printFunc 时重用它(我们看到相同的哈希码)。当我们使用 def 时,每当我们将它传递给 printFunc 时,转换为函数就会发生,并且我们创建了多个实例具有不同哈希码的函数。 试试看



也就是说,性能开销很小,并且通常对我们的程序没有什么实际影响,所以经常使用 def 来定义函数,因为许多人发现它们更简洁,更易读。 / p>

I'm learning Programming Paradigms in my University and reading this course material provided by the lecturer that defined a function this way:

val double = (x: Int) => 2 * x
double: Int => Int = <function1>

But from my own studies I found and got used to defining the same function like this:

def d (x: Int) = 2 * x
d: (x: Int)Int

I'm new to Scala. And both definitions give a result of:

res21: Int = 8

Upon passing 4 as the parameter. Now my main question is why would the lecturer prefer to use val to define a function? I see it as longer and not really necessary unless using val gives some added advantages that I don't know of. Besides I understand using val makes some name a placeholder so later in the program, I could mistakenly write val double = 5 and the function would be gone! At this stage I'm quite convinced I learned a better way of defining a function unless someone would tell me otherwise.

解决方案

Strictly speaking def d (x: Int) = 2 * x is a method, not a Function, however scala can transparently convert (lift) methods into Functions for us. So that means you can use the d method anywhere that requires a Int => Int Function.

There is a small overhead of performing this conversion, as a new Function instance is created every time. We can see this happening here:

val double = (x: Int) => 2 * x
def d (x: Int) = 2 * x

def printFunc(f: Int => Int) = println(f.hashCode())

printFunc(double)
printFunc(double)
printFunc(d)
printFunc(d)

Which results in output like so:

1477986427
1477986427
574533740
1102091268

You can see when explicitly defining a Function using a val, our program only creates a single Function and reuses it when we pass as an argument to printFunc (we see the same hash code). When we use a def, the conversion to a Function happens every time we pass it to printFunc and we create several instances of the Function with different hash codes. Try it

That said, the performance overhead is small and often doesn't make any real difference to our program, so defs are often used to define Functions as many people find them more concise and easier to read.

这篇关于用于定义Scala中的Function的def或val的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆