The syntax of currying in scala is for example
def f(x: Int, b: Int) = x + y
is
def f(x: Int)(b: Int) = x + y
And curr
(Int, Int) => Int
between :
and =
specify the function's return type, i.e, it says sum
will return another method of signature (Int, Int) => Int
which takes two Int
and returns another Int
, and this is the signature of your inner sumF
function:
You can rewrite this to the currying syntax as follows:
def sum(f: Int => Int)(a: Int, b: Int): Int = {
def sumF(a: Int, b: Int): Int =
if (a > b) 0
else f(a) + sumF(a + 1, b)
sumF(a, b)
}
This will more or less do the same thing as the method defined in OP:
sum(x => x)(3, 6)
// res11: Int = 18
But these two definitions are not exactly the same, for instance, for the currying syntax defined here, if you want to generate a new method from it, you have to specify the variable type, like:
val mysum: (Int, Int)=> Int = sum(x => x)
But for the one in OP, it can be simply val mysum = sum(x => x)
as the return type of sum
as already been specified.
Your sum
example is not curried. If you wanted it curried you'd do something like:
def sum(f: Int => Int)(a: Int, b: Int): Int =
if (a > b) 0
else f(a) + sum(f)(a + 1, b)
sum(x=>x)(3, 6) // res0: Int = 18
Your code defines a method that takes a single argument, def sum(f: Int => Int)
. That argument is a function that takes and Int
and returns an Int
. So no currying involved.
This sum
method returns a function, : (Int, Int) => Int
. This returned function takes 2 Int
s and returns and Int
. Invoking this sum
method looks like currying, but it's not.
sum(x=>x)(3, 6)
Instead you are invoking sum()
with a single argument (x=>x
) and then invoking the returned function with two arguments (3,6
).