Scala Higher-Order Functions


Scala allows the definition of higher-order functions. These are functions that take other functions as parameters, or whose result is a function. For example in the following code, apply() function takes another function f and a value v and applies function f to v:

object Test {
   def main(args: Array[String]) {

      println( apply( layout, 10) )


   def apply(f: Int => String, v: Int) = f(v)

   def layout[A](x: A) = "[" + x.toString() + "]"

When the above code is compiled and executed, it produces the following result:

C:/>scalac Test.scala
C:/>scala Test