Higher-order functions

In Scala's functional programming, you are allowed to pass functions as parameters and even return a function as a result from another function; this defines what are called higher-order functions.

Let's demonstrate this feature by an example. Consider the following function testHOF that takes another function func and then applies this function to its second argument value:

object Test {  def main(args: Array[String]) {    println( testHOF( paramFunc, 10) )  }  def testHOF(func: Int => String, value: Int) = func(value)  def paramFunc[A](x: A) = "[" + x.toString() + "]"}

After demonstrating the basics of Scala's functional programming, now we are ready to move to more complex cases of functional programming. As mentioned earlier, ...

Get Scala and Spark for Big Data Analytics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.