A function is called Higher Order Function if it contains other functions as a parameter or returns a function as an output i.e, the functions that operate with another functions are known as Higher order Functions. It is worth knowing that this higher order function is applicable for functions and methods as well that takes functions as parameter or returns a function as a result. This is practicable as the compiler of Scala allows to force methods into functions.
Some important points about Higher order functions:
- The Higher order functions are possible, as Scala programming language acts towards the functions as first-class values, which implies that analogous to some other values, functions can even be passed as a parameter or can be returned as an output, which is helpful in supplying an adjustable method for writing codes.
- It is beneficial in producing function composition where, functions might be formed from another functions. The function composition is the method of composing where a function shows the utilization of two composed functions.
- It is also constructive in creating lambda functions or anonymous functions. The anonymous functions are the functions which does not has name, though perform like a function.
- It is even utilized in minimizing redundant lines of code from a program.
Now, lets see some examples.
-
Example :
// Scala program of higher order// function// Creating objectobjectGfG{// Main methoddefmain(args:Array[String]){// Displays output by assigning// value and calling functionsprintln(apply(format,32))}// A higher order functiondefapply(x:Double=>String, y:Double)=x(y)// Defining a function for// the format and using a// method toString()defformat[R](z:R)="{"+ z.toString() +"}"}chevron_rightfilter_noneOutput:{32.0}Here, the apply function contains an another function x with a value y and applies the function x to y.
-
Example :
// Scala program of higher order// function// Creating objectobjectGfG{// Main methoddefmain(args:Array[String]){// Creating a list of numbersvalnum=List(7,8,9)// Defining a methoddefmultiplyValue=(y:Int)=>y *3// Creating a higher order function// that is assigned to the variablevalresult=num.map(y=>multiplyValue(y))// Displays outputprintln("Multiplied List is: "+ result)}}chevron_rightfilter_noneOutput:Multiplied List is: List(21, 24, 27)
Here, map is a function that takes another function i.e, (y => multiplyValue(y)) as a parameter so, it is a higher order function.
Recommended Posts:
- Kotlin Higher-Order Functions
- Inverse functions and composition of functions
- Fruitful Functions and Void Functions in Julia
- Scala | Partially Applied functions
- Scala | Functions Call-by-Name
- Using anonymous functions with the map method in Scala
- Scala | Functions - Basics
- Currying Functions in Scala with Examples
- Anonymous Functions in Scala
- Scala | Nested Functions
- Partial Functions in Scala
- Scala Tutorial – Learn Scala with Step By Step Guide
- Mathematics | Generating Functions - Set 2
- Functions in Python
- PHP | startsWith() and endsWith() Functions
- Functions of Operating System
- jQuery | Callback Functions
- Perl | List Functions
- How to add many functions in one ng-click directive?
- ES6 | Functions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

