Scala Higher-Order Functions

Scala 函数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 parameters, or use functions as output results.

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

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

      println( apply( layout, 10) )

   }
   // 函数 f 和 值 v 作为参数,而函数 f 又调用了参数 v
   def apply(f: Int => String, v: Int) = f(v)

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

The output of executing the above code is:

$ scalac Test.scala
$ scala Test
[10]

Scala 函数Scala Functions

Other Extensions