Right Arrow meanings in Scala
Asked Answered
A

3

29

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 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.

I don't understand what is "Double => Double" here, in previous chapters, where "=>" appears only means function literal, and never wrote like this "Type => Type", because according to Scala function literal syntax definition, the right part of function literal is the function body, how can a function body be "Double" ?

Arcature answered 7/10, 2010 at 2:54 Comment(0)
F
55

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 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 = <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.

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.

Fricandeau answered 7/10, 2010 at 3:36 Comment(0)
L
15

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:

T => R means Function1[T, R]

(T1, T2) => R means Function2[T1, T2, R]

...

In your example, this means that op is a function that takes a Double and returns a Double (as the author explained).

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
Laine answered 7/10, 2010 at 3:30 Comment(2)
Why are you enumerating Function? (as in FunctionN, Function1, etc)Bashemeth
@jcora - FunctionN is a trait in Scala, Function1 is a type of that trait that represents a function with 1 argument - check out gleichmann.wordpress.com/2010/11/08/… and mikefroh.blogspot.co.uk/2011/12/…Kaczmarek
O
2

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]

Overset answered 7/10, 2010 at 3:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.