Hello All -
I would like to understand the difference between the Convention way of function and Higher-Order function.
Let me go with an example, X Cube + Y For the instance my inputs are X= 2 and Y = 5 so the expected result is 13. I'm able to implement the code in both the ways i.e 13
It would be great if you could benefit and advantage of Higher-Order function. And please correct me if my way of implementing the Solution # 2 is not correct.
// Solution # 1
object funXcubeplusY_normal extends App {
println(sum(cube(2),5))
def sum(x:Int,y:Int):Int= x+y
def cube(x:Int):Int= x*x*x
}
Output:
13
//Solution # 2
object funXcubePlusY_HigherOrder extends App {
println("Sum of 2 Cube plus 5 = " + sum(cube(funXcubePlusY_HigherOrder.fncube,2),5))
def sum(x:Int,y:Int):Int = x + y
def cube(m:Int=> Int,n:Int)= m(n)
def fncube[m](m:Int):Int= m*m*m
}
Ouput:
Sum of 2 Cube plus 5 = 13