def sum(f: Int => Int): (Int, Int) => Int = {
def sumF(a: Int, b: Int): Int =
if (a > b) 0
else f(a) + sumF(a + 1, b)
sumF
}
def sumCubes = sum(a => a * a * a)
sumCubes // output Function2
sumCubes(1,10) // output 3025.
sumCubes() // doesn't work
I feel I dont understand currying well enough. In the first statement , we are calling sumCubes
without parameters , hence sum
gets called with the anonymous function as parameter and returns a function2.
Whats really happening in 2nd and 3rd invocation , Why are we able to do
sum(a => a * a * a)(1,10)
but not
sumCubes()(1,10)
My understanding is that in sum(a => a * a * a)(1,10)
, we are partially applying sum
to the anonymous function, which returns a Function2 ,which is applied to the second pair of parameters (1,10) and hence we are getting 3025,
However the same should happen in case of sumCubes()(1,10)
, invoking sumCubes
without parameters first , would inturn invoke sum
with the anonymous function and the Function2 returned would be applied to (1,10)
Why does sumCubes(1,10)
work but not sumCubes()(1,10)
, shouldn't sumCubes
and sumCubes()
mean the same thing , invocation of function sumCubes
. Also if a simple reference sumCubes
is invoking it , how can I pass it around. I feel like I am not understanding something fundamental about Scala.