Abhi Abhi - 7 months ago 37
Scala Question

Invoking higher order functions - Scala

Hi I am new to Scala and trying to call a higher order function sum_of from main class.I am getting "Cannot resolve reference sumOf with such signature error".

object SumOf {

def main(args: Array[String]) {
val y = sumOf(x=>x ,4,5)

def sumOf(f: Int => Int)(a: Int, b: Int): Int = {
def loop(a: Int, acc: Int): Int =
if (a > b) acc
else loop(a + 1, f(a) + acc)
loop(a, 0)



Your sumOf method has two argument lists, and needs to be called with two argument lists.

val y = sumOf(x => x)(4, 5)

You can think of sumOf as a function which takes an Int => Int and returns a new function, which takes two Ints to return an Int.