在Scala中定义函数的两种方法。有什么不同? [英] Two ways of defining functions in Scala. What is the difference?
问题描述
scala>这是一个小小的Scala会话,它定义并尝试了一些函数。 def test1(str:String)= str + str;
test1:(str:String)java.lang.String
scala> test1(ab)
res0:java.lang.String = abab
。
scala> val test2 = test1
< console>:6:错误:对象$ iw中缺少方法test1的参数;
如果您想将其视为部分应用函数,则使用`_'跟随此方法
val test2 = test1
^
oops。
scala> val test2 = test1 _
test2:(String)=> java.lang.String =< function1>
scala> test2(ab)
res1:java.lang.String = abab
效果很好!
现在,我在折叠时看到了 _
语法( _ + _
等)。据我所知, _
基本上是指一个参数。所以 test1 _
基本上意味着一个带有参数的函数,它被赋予 test1
。
所以我一直在探索......
scala> val test3 =(str:String)=> str + str
test3:(String)=> java.lang.String =< function1>
scala> test3(ab )
res2:java.lang.String = abab
scala> val test4 = test3
test4:(String)=> java.lang.String =< function1>
这里没有 _
! def
ed函数与 val
ed函数之间的区别?
def'ed函数和val'ed函数之间没有区别:
scala> def test1 =(str:String)=> str + str
test1:(String)=> j ava.lang.String
scala> val test2 = test1
test2:(String)=> java.lang.String =< function1>
scala> val test3 =(str:String)=> str + str
test3:(String)=> java.lang.String =< function1>
scala> val test4 = test2
test4:(String)=> java.lang.String =< function1>
请参阅?所有这些都是函数,它由 X => Y
类型。
scala> def test5(str:String)= str + str
test5:(str:String)java.lang.String
您是否看到 X => Y
键入?如果你这样做,去看眼科医生,因为没有。这里的类型是(X)Y
,通常用来表示一个方法。
实际上, test1
, test2
, test3
和 test4
都是返回函数的方法。 test5
是一个返回 java.lang.String
的方法。另外, test1
至 test4
不接受参数(只有 test1
可以,无论如何),而 test5
不会。
所以,区别非常简单。在第一种情况下,您尝试将方法分配给val,但未填写方法所需的参数。所以它失败了,直到你添加了一个尾部下划线,这意味着把我的方法变成一个函数。
在第二个例子中,你有一个函数,所以你不需要做任何事情。方法不是函数,反之亦然。函数是 FunctionN
类之一的对象。一个方法是与某个对象相关的一段代码的句柄。
查看堆栈溢出中有关方法与函数的各种问题。
Here is a little Scala session that defines and tries out some functions:
scala> def test1(str: String) = str + str;
test1: (str: String)java.lang.String
scala> test1("ab")
res0: java.lang.String = abab
works nicely.
scala> val test2 = test1
<console>:6: error: missing arguments for method test1 in object $iw;
follow this method with `_' if you want to treat it as a partially applied function
val test2 = test1
^
oops.
scala> val test2 = test1 _
test2: (String) => java.lang.String = <function1>
scala> test2("ab")
res1: java.lang.String = abab
works well!
Now, I've seen the _
syntax when folding (_ + _
, etc). So as I understand it _
basically means "an argument". So test1 _
basically means a function with an argument, which is given to test1
". But why isn't that exactly the same as just test1
? Why is there a difference if I append a _
?
So I kept exploring...
scala> val test3 = (str: String) => str + str
test3: (String) => java.lang.String = <function1>
scala> test3("ab")
res2: java.lang.String = abab
scala> val test4 = test3
test4: (String) => java.lang.String = <function1>
Here it works without _
! What's the difference between a def
ed function, and a val
ed function?
There's no difference between a def'ed function and a val'ed function:
scala> def test1 = (str: String) => str + str
test1: (String) => java.lang.String
scala> val test2 = test1
test2: (String) => java.lang.String = <function1>
scala> val test3 = (str: String) => str + str
test3: (String) => java.lang.String = <function1>
scala> val test4 = test2
test4: (String) => java.lang.String = <function1>
See? All of these are functions, which is indicated by the X => Y
type they have.
scala> def test5(str: String) = str + str
test5: (str: String)java.lang.String
Do you see an X => Y
type? If you do, go see an ophthalmologist, because there's none. The type here is (X)Y
, commonly used to denote a method.
Actually, test1
, test2
, test3
and test4
are all methods, which return functions. test5
is a method which returns a java.lang.String
. Also, test1
through test4
do not take parameters (only test1
could, anyway), while test5
does.
So, the difference is pretty simple. In the first case, you tried to assign a method to a val, but did not fill in the parameters the method take. So it failed, until you added a trailing underscore, which meant turn my method into a function.
In the second example you had a function, so you didn't need to do anything else.
A method is not a function, and vice versa. A function is an object of one of the FunctionN
classes. A method is a handle to some piece of code associated with an object.
See various questions about methods vs functions on Stack Overflow.
这篇关于在Scala中定义函数的两种方法。有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!