English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Scala Higher-order Functions

Scala Functions

Higher-order Function(高阶函数)-Order Function) is a function that operates on other functions.

Scala allows the use of higher-order functions, which can use other functions as parameters or use functions as output results.

In the following example, the apply() function uses another function f and value v as parameters, and the function f calls the parameter v:

object Test {
   def main(args: Array[String]) {
      println( apply( layout, 10) )
   }
   // The function f and the value v are passed as parameters, and the function f calls the parameter v
   def apply(f: Int => String, v: Int) = f(v)
   def layout[A](x: A) = "[" + x.toString() + "]"
}

Executing the above code, the output will be:

$ scalac Test.scala
$ scala Test
[10]

Scala Functions