Abhi Abhi - 1 year ago 74
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)


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download