In Chapter 9 of Programming In Scala, there is an example method like this:
def twice(op: Double => Double, x: Double) = op(op(x))
The
Because it has two usages.
First, you could use => to define function literal.
scala> val fun = (x: Double) => x * 2
fun: (Double) => Double =
scala> fun (2.5)
res0: Double = 5.0
It's pretty easy. But the question here is, what type fun is? It is a "function that takes a Double as an argument and return a double", right?
So how could I annotate fun with its type? That is (Double) => (Double). Well, the previous example could be rewritten to:
scala> val fun: Double => Double = (x: Double) => x * 2
fun: (Double) => Double =
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.
So you could pass the previous fun function to its first argument.
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 op with fun, and replace x with 10, that is fun(fun(10)) and the result will be 40.