匿名Scala函数语法 [英] Anonymous Scala function syntax
问题描述
I'm learning more about Scala, and I'm having a little trouble understanding the example of anonymous functions in http://www.scala-lang.org/node/135. I've copied the entire code block below:
object CurryTest extends Application {
def filter(xs: List[Int], p: Int => Boolean): List[Int] =
if (xs.isEmpty) xs
else if (p(xs.head)) xs.head :: filter(xs.tail, p)
else filter(xs.tail, p)
def modN(n: Int)(x: Int) = ((x % n) == 0)
val nums = List(1, 2, 3, 4, 5, 6, 7, 8)
println(filter(nums, modN(2)))
println(filter(nums, modN(3)))
}
我对modN函数的应用感到困惑
I'm confused with the application of the modN function
def modN(n: Int)(x: Int) = ((x % n) == 0)
在此示例中,它使用一个参数调用
In the example, it's called with one argument
modN(2) and modN(3)
modN(n:Int)(x:Int)的语法是什么意思?
What does the syntax of modN(n: Int)(x: Int) mean?
由于它是用一个参数调用的,因此我假设它们不是两个参数,但是我真的无法弄清楚mod函数如何使用nums的值.
Since it's called with one argument, I'm assuming they're not both arguments, but I can't really figure out how the values from nums get used by the mod function.
推荐答案
这在函数式编程中很有趣,称为 currying .基本上,MosesSchönfinkel和后来的Haskell Curry(虽然Schonfinkeling听起来很奇怪...)提出了这样的想法,即调用多个参数的函数,例如f(x,y)
与调用链{g(x)}(y)
或g(x)(y)
相同,其中g
是一个产生另一个函数作为其输出的函数.
This is a fun thing in functional programming called currying. Basically Moses Schönfinkel and latter Haskell Curry (Schonfinkeling would sound weird though...) came up with the idea that calling a function of multiple arguments, say f(x,y)
is the same as the chain of calls {g(x)}(y)
or g(x)(y)
where g
is a function that produces another function as its output.
以功能f(x: Int, y: Int) = x + y
为例.如预期的那样,调用f(2,3)
会产生5
.但是,当我们使用此函数时会发生什么-将其重新定义为f(x:Int)(y: Int)
并将其命名为f(2)(3)
.第一个调用f(2)
生成一个函数,该函数采用整数y
并向其中添加2
->因此,f(2)
具有类型Int => Int
,并且等效于函数g(y) = 2 + y
.第二个调用f(2)(3)
用参数3
调用新产生的函数g
,因此按预期计算为5
.
As an example, take the function f(x: Int, y: Int) = x + y
. A call to f(2,3)
would produce 5
, as expected. But what happens when we curry this function - redefine it as f(x:Int)(y: Int)
and call it as f(2)(3)
. The first call, f(2)
produces a function taking an integer y
and adding 2
to it -> therefore f(2)
has type Int => Int
and is equivalent to the function g(y) = 2 + y
. The second call f(2)(3)
calls the newly produced function g
with the argument 3
, therefore evaluating to 5
, as expected.
查看它的另一种方法是逐步执行f(2)(3)
调用的减少(功能程序员将其称为beta减少-就像逐行执行功能的方法)(请注意,以下内容并非真正有效的Scala)语法).
Another way to view it is by stepping through the reduction (functional programmers call this beta-reduction - it's like the functional way of stepping line by line) of the f(2)(3)
call (note, the following is not really valid Scala syntax).
f(2)(3) // Same as x => {y => x + y}
|
{y => 2 + y}(3) // The x in f gets replaced by 2
|
2 + 3 // The y gets replaced by 3
|
5
因此,在所有这些讨论之后,f(x)(y)
可以被视为只是以下lambda表达式(x: Int) => {(y: Int) => x + y}
-这是有效的Scala.
So, after all this talk, f(x)(y)
can be viewed as just the following lambda expression (x: Int) => {(y: Int) => x + y}
- which is valid Scala.
我希望这一切都有道理-我试图给出为什么 modN(3)
调用有道理的背景:)
I hope this all makes sense - I tried to give a bit of a background of why the modN(3)
call makes sense :)
这篇关于匿名Scala函数语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!