Skip to content

Scala FunctionX

Resources

Basics

Function1[Int, Int]
[Int <- Type of first argument, Int <- return type]

Function2[String, String, String]
[String <- Type of first argument, String <- Type of second
argument, <- return type]

From this course lecture:

All Scala functions are instances of FunctionX types.

val doubler: Function1[Int, Int] = new Function1[Int, Int] {
override def apply(x: Int): Int = 2 * x
}

doubler(4) // 8

// syntantic sugar for the above
val doubler2: Function1[Int, Int] = (x: Int) => 2 * x
// or skip the type
val doubler3 = (x: Int) => 2 * x