Scala 中右箭头的含义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3878347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Right Arrow meanings in Scala
提问by Sawyer
In Chapter 9 of Programming In Scala, there is an example method like this:
在 Programming In Scala 的第 9 章中,有一个这样的示例方法:
def twice(op: Double => Double, x: Double) = op(op(x))
The author said in the book:
作者在书中说:
The type of op in this example is Double => Double, which means it is a function that takes one Double as an argument and returns another Double.
本例中op的类型是Double => Double,这意味着它是一个以一个Double为参数并返回另一个Double的函数。
I don't understand what is "Double => Doulbe" here, in previous chapters, where "=>" appears only means function literal, and never wrote like this "Type => Type", because according to scala function literal syntax defination, the right part of function literal is the function body, how can a function body be "Double" ?
我不明白这里的“Double => Doulbe”是什么,在前面的章节中,出现“=>”的地方只表示函数字面量,从来没有像“Type => Type”这样写过,因为根据scala函数字面量语法定义, 函数字面量的右边是函数体,函数体怎么能是“双”呢?
回答by Brian Hsu
Because it has two usages.
因为它有两种用法。
First, you could use => to define function literal.
首先,您可以使用 => 来定义函数字面量。
scala> val fun = (x: Double) => x * 2
fun: (Double) => Double = <function1>
scala> fun (2.5)
res0: Double = 5.0
It's pretty easy. But the question here is, what type funis? It is a "function that takes a Double as an argument and return a double", right?
这很容易。但这里的问题是,什么类型fun?它是一个“将 Double 作为参数并返回 double 的函数”,对吗?
So how could I annotate fun with its type? That is (Double) => (Double). Well, the previous example could be rewritten to:
那么我怎么能用它的类型来注释乐趣呢?那就是(Double) => (Double)。好吧,前面的例子可以改写为:
scala> val fun: Double => Double = (x: Double) => x * 2
fun: (Double) => Double = <function1>
scala> fun (2.5)
res1: Double = 5.0
OK, then what does the following code do?
好的,那么下面的代码是做什么的呢?
def twice(op: Double => Double, x: Double) = op(op(x))
Well, it tells you that op is a (Double => Double), which means it needs a function which takes a Double and return a Double.
好吧,它告诉你 op 是 a (Double => Double),这意味着它需要一个函数,它接受一个 Double 并返回一个 Double。
So you could pass the previous funfunction to its first argument.
所以你可以将前一个fun函数传递给它的第一个参数。
scala> def twice(op: Double => Double, x: Double) = op(op(x))
twice: (op: (Double) => Double,x: Double)Double
scala> twice (fun, 10)
res2: Double = 40.0
And it will be equivalent to replacing opwith fun, and replace xwith 10, that is fun(fun(10))and the result will be 40.
并且等价于替换op为fun,替换x为10,即fun(fun(10))结果为 40。
回答by Aaron Novstrup
In addition to separating the parameter list from the function body in a function literal, the double arrow can be used as syntactic sugar for a FunctionN type:
除了在函数字面量中将参数列表与函数体分开,双箭头还可以用作 FunctionN 类型的语法糖:
T => Rmeans Function1[T, R]
T => R方法 Function1[T, R]
(T1, T2) => Rmeans Function2[T1, T2, R]
(T1, T2) => R方法 Function2[T1, T2, R]
...
...
In your example, this means that opis a function that takes a Double and returns a Double (as the author explained).
在您的示例中,这意味着这op是一个接受 Double 并返回 Double 的函数(如作者所解释的)。
As another example, it's possible to write
再举一个例子,可以这样写
// declare len as a function that takes a String and returns an Int,
// and define it as returning the length of its argument
val len: String => Int = { _.length }
// or, a more verbose version that uses '=>' in both ways
val len: String => Int = { (s: String) => s.length }
len("apple") // returns 5
回答by fedesilva
That is a function that takes a double and returns a double.
这是一个接受双精度值并返回双精度值的函数。
scala> def d2d = (d:Double) => d
d2d: (Double) => Double
It's type is Function1[Double,Double]
它的类型是 Function1[Double,Double]

