Topic | High Order Functions |
---|---|
Benefits realized | How High Order Functions works in Scala |
Git sample | HighOrderFunctionTest.scala |
References | docs.scala-lang.org |
A Higher-Order function is a function that does at least one of the following:
- Takes one or more functions as an input
- Outputs a function
In example below, there is a function:
apply
which takes another functionf
and a valuev
and applies functionf
tov
Function:
apply
consumes anInt
and returns aString
:Int => String
In example below, method
decorator.layout
is coerced automatically to a value of typeInt => String
as required by methodapply
object HighOrderFunctionTest extends App {
/* Method `apply` has two params: function `f` & value `v` and it applies `f` to `v` */
def apply(f: Int => String, v: Int) = f(v)
/* Create a new 'Decorator' object */
val decorator = new Decorator("[", "]")
/* Pass 'decorator.layout' to 'apply' */
val layoutOutput = apply(decorator.layout, 7)
println(layoutOutput)
}
class Decorator(left: String, right: String) {
/* Method is bound to type 'A' */
def layout[A](x: A) = left + x.toString() + right
}