Reputation: 34061
I have following function with parameter lists:
def foo(a:Int, b:Int)(x:Int)(y:Int) = a * b + x - y
when I put the command on the relp
foo _
it shows me
res5: (Int, Int) => Int => (Int => Int) = <function2>
The first part expects two int parameters and then I do not know anymore how to read continue.
I can use the function like
foo(5,5)(10)(10)
but I do not know how to read it!
Upvotes: 1
Views: 79
Reputation: 2069
It's easy. (Int, Int) => Int => (Int => Int) means that 2 first steps returns a function that will take some arguments. For example:
(Int, Int) => Function that return
(Int) => Function that return
(Int) => Int
Why is it useful? Because when you have a function that take a function as parameter in of it's arguments list you can omit parentheses and use curly braces.
def someFunStuff(fun: Int)(stuff: Int => Int) = {
fun*stuff(fun)
}
someFunStuff(2) { x =>
//Do some fun stuff
x * 2 / x
}
If you want to know a little bit more, please read Chapter 9.4 programming in Scala second edition.
Upvotes: 1
Reputation: 1637
In general, A => B is the type of an anonymous function that, when given an argument of
type A, returns a value of type B.
Now, the type
(Int, Int) => Int => (Int => Int)
might look less confusing when you add some parenthesis:
(Int, Int) => (Int => (Int => Int))
As a start, just break it down to
(Int, Int) => (… => …)
i.e. a function that, given two Int
, returns another function. The
function that is returned has type
(Int => (Int => Int))
i.e. a function that, given an Int, returns another function that, given an Int, returns an Int.
Upvotes: 2