1.

Explain higher-order functions with an example.

Answer»

Higher-order functions are functions in Scala that either take or return another function as an ARGUMENT or parameter. Another way to say it is a function that works with a function. You can create higher-order functions, lambda functions, or anonymous functions using a higher-order function.  

Example: In this case, the apply function contains another function a that is APPLIED to B

 OBJECT InterviewBit { def main(args: Array[String]) { println(apply(format, 45)) } def apply(a: DOUBLE => String, b: Double) = a(b) def format[S](x: S) = "{" + x.toString() + "}" }

Output: 

 {45.0}


Discussion

No Comment Found