Easy Tutorial
❮ Scala Maps Nested Functions ❯

Scala Higher-Order Functions

Scala Functions

A higher-order function is a function that operates on other functions.

Scala allows the use of higher-order functions, which can take other functions as arguments or return a function as a result.

In the following example, the apply() function takes another function f and a value v as arguments, and the function f in turn calls the argument v:

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

      println( apply( layout, 10) )

   }
   // Function f and value v as arguments, and function f calls the argument v
   def apply(f: Int => String, v: Int) = f(v)

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

}

Executing the above code, the output result is:

$ scalac Test.scala
$ scala Test
[10]

Scala Functions

❮ Scala Maps Nested Functions ❯