Updated April 4, 2023
Introduction to Scala High Order Functions
High Order Functions, let us first look on what high ordered functions are. A function that takes on other functions as a parameter or in the result returns a function are basically known as High Order Functions. We can pass a function as an argument of other function. Or simply can wait for Scala function to return the function.
We can also define our own High Order Functions by just defining a function and passing that function to other functions to achieve the desired result. In Object-Oriented programming when a method is parameterized with a function it generally can leak the object’sinternal state breaking the encapsulation property for that object. Here internal state refers to the internal property of an object. A map can be the best example that uses Scala High Order Functions. How Map uses that we will check that with an example later.
Definitely using High Order Functions minimize the lines of code making the code easier to understand and clean. We can also achieve function composition with the help of High Order Functions, Function composition comes with the part that shows the utilization of two composed functions.
We can also make anonymous function, function having no name still being used as function and lambda function with the help of this Scala High Order Functions.
Now let us see what an anonymous function is, sometimes there are lots of small functions that makes it tedious for the programmer to name and define each and every function so we make use of anonymous function.
Example
let us check with an example:-
object Demo {
def main(args: Array[String]) {
val str = "abc";
println(str)
}
}
We can directly write this as-
object Demo {
def main(args: Array[String]) {
println("abc")
}
}
String being an Literal allows us to write the function without any name these are known as Anonymous Functions.
Function Syntax
(x: Int ) => x*x
where x:int is the parameter and x*x is the body here. Now let us check for some example for Scala High Order Functions and look how it works programmatically. As discussed Map operation is one of the classic example for High Order Functions.
Code:
object Demo {
def main(args: Array[String]) {
val list = List(2,3,4)
println(list)
}
}
object Demo {
def main(args: Array[String]) {
val list = List(2,3,4)
def sum =(x:Int) => x+2 // Defining a Function sum that adds 2 with the required functions
def mulitply = (x: Int ) => x*2 //Defining another function that multiplies with the required function.
val cList = list.map(x => sum(x)) // Using the Function with Map Function
println(cList)
}
}
object Demo {
def main(args: Array[String]) {
val list = List(2,3,4)
//println(list)
def sum =(x:Int) => x+2 // Defining a Function sum that adds 2 with the required functions
def mulitply = (x: Int ) => x*2 //Defining another function that multiplies with the required function.
val cList = list.map(x =>mulitply(x)) // Using again the same function
println(cList)
}
}
here we can see that from the above code within the Map Function we used the function that was defined and map Function uses that function value to all the elements in the list. We also can create our own Higher Order Function let us check with an example:-
Code:
object Demo {
def main(args: Array[String]) {
def add(f: (Int, Int) => Int,a: Int, b:Int): Int = f(a,b)
val squareSum = (x: Int, y: Int) => (x*x + y*y)
println(squareSum)
}
}
object Demo {
def main(args: Array[String]) {
def add(f: (Int, Int) => Int,a: Int, b:Int): Int = f(a,b)
val squareSum = (x: Int, y: Int) => (x*x + y*y)
val squaredSum = add(squareSum, 1, 2)
println(squaredSum)
}
}
Here we can see that the function add takes the function Square sum as the parameter defined by us and gives the required results over. The function first goes to the square sum value with the values assigned to them and then do the operations over the every element needed.
Passing Lambda Expression as the High Order Functions
We can create an anonymous function either by using => or _ wild card in scala.
Code:
object Demo {
def main(args: Array[String]) {
var result = (a:Int ,b:Int ) => a+b // here we made an anonymous function using =>
println(result)
}
}
object Demo {
def main(args: Array[String]) {
var result1 = (_:Int)+(_:Int) //here using it as _ (underscore)
println(result1(1,2))
}
}
object Demo {
def main(args: Array[String]) {
var result = (a:Int ,b:Int ) => a+b // here we made an anonymous function using =>
var result1 = (_:Int)+(_:Int) //here using it as _ (underscore)
println(result(1,2))
}
}
Here we will can see that we can make an anonymous function and call that back with the same value over.
Benefits of Using Anonymous Functions With Scala
With the help of Anonymous Function we can separate different pieces of functionality. A higher order function using anonymous function doesn’t knows that how the function work, this allows the flexibility to compute arguments and concerned only with these arguments used and the return types. Lambda expression are simply the reference to the Anonymous Functions.
Conclusion
Here from the above article, we checked that the use of high order function for Scala, a function taking other function as an argument or returning the function over there can be called as High Order Function making the code base easy and understandable for operations.
Also, we saw the examples showing how can we use these high order functions in different ways. Also, we get to know about the anonymous functions how we can also pass them as the high order functions over.
Recommended Articles
We hope that this EDUCBA information on “Scala High Order Functions” was beneficial to you. You can view EDUCBA’s recommended articles for more information.