Previous

Next

Tour of Scala

# Higher-order Functions¶

Higher order functions take other functions as parameters or return a function as a result. This is possible because functions are first-class values in Scala. The terminology can get a bit confusing at this point, and we use the phrase "higher order function" for both methods and functions that take functions as parameters or that return a function.

One of the most common examples is the higher-order function map which is available for collections in Scala.

In [1]:
val salaries = Seq(20000, 70000, 40000)
val doubleSalary = (x: Int) => x * 2
val newSalaries = salaries.map(doubleSalary) // List(40000, 140000, 80000)

Out[1]:
salaries: Seq[Int] = List(20000, 70000, 40000)

Previous

Next

Tour of Scala