First-Class Functions and Lambdas

Functions in Scala are first-class values. You can assign them to variables, pass them as parameters, and return them.

Function Literal (Lambda)

val add = (a: Int, b: Int) => a + b
println(add(2, 3))

Passing Functions

def operate(f: Int => Int, x: Int): Int = f(x)
val square = (x: Int) => x * x
println(operate(square, 4))
← PrevNext →