Updated April 20, 2023
Introduction to Scala Anonymous Function
A function that does not contain any name or a function without a name is called as anonymous function in scala. This anonymous function is also called a function literal. Inside the anonymous function, we can write our logic and defined our function. By the use of the anonymous function, we can create a very lightweight function also if we can create an inline function.
Syntax
Below you can find the syntax for defining anonymous function where we use arrow (=>) for defining the body of our function. This arrow itself represents an anonymous function. We will also see one practice example for beginners for the better understating of the functions see below;
(variable_name: Data_type) => Our logic goes here..
Example:
(a: String) => a + "Hello !!"
In the above lines of code, we are passing a string variable to the function and appending some other string as well by using an anonymous function. So this is the very lightweight syntax for defining and using a function in scala.
How does Anonymous Function work in Scala?
As we know anonymous functions are also called function literals and these functions literal and their object is known as the function value. While working with the scala anonymous function we use arrow (=>) symbol and this symbol is a transformer in function. This transformer is responsible to transmit the parameter from the left-hand side to the right-hand side and there we can perform any operation on our inputs, hence here we are evaluating out parameters. It produces a new result on the right-hand side based on the logic or the expression that we have defined there.
We can define the anonymous function in two ways let’s discuss them one y one see below;
1) using ‘_’: We can also define the anonymous function by using the wildcard character ‘_’ this characterrepresent the parameter which appears only once.
syntax:
(_:Double)*(Double)
2) using ‘=>’: This is the most common way to define the anonymous function. This arrow we can say act as a callback here which is also responsible to transmit the parameter to the logic and evaluates them and provide us the result.
syntax:
(a: String) => a + "Hello !!"
We have lambda function in scala which are anonymous function we can have a look at them also they work in detail see below;
Scala lambda functions are anonymous function. They reduce the line of code and make the function more readable and convenient to define. We can also reuse them. We can use this lambda function to iterate our collection data structure and performed and kind of operations on them.
Example:
var list = List(100, 200, 345, 67, 267, 195)
var result = list.filter((a: Int) => a % 5 == 0)
In the above lines of code, we are creating a list of integers. After that, we are applying filter method and passing an anonymous function inside it. So every element of list which is divisible by 5 will be retained and return as the new list into the result variable. So in this way, we can use them and pass them inside any function. Suppose we want to apply some transformation of the collection elements for this we have one method available named as map (). By the use of this method, we can transform every element of the collection data structure in scala. This returns us with the new list of elements with modified values. Let’s see one example for beginners for better understanding see below;
Example:
val list = List(100, 200, 345, 67, 267, 195)
val result = list.map( (a:Int) => a + 10 )
In the above lines of code, we are creating one list of integers followed by the map function in scala. By using the map () method we are transforming the elements of collection data structure and increasing each element in the list by 10. This will return us the new list with modified elements. We can also define the anonymous function and pass that function into the map function it is not mandatory that we have to define inside the map only.
Anonymous functions are of two types which are as follow;
1) Anonymous function without a parameter: We can define our anonymous function without parameter and pass one function inside another.
2) Anonymous function with parameter: In this, we can pass any number of parameters in an anonymous function.
Examples of Scala Anonymous Function
Here are the following examples mention below :
Example #1
In this example, we are using a transform to define our anonymous function.
Code:
object Main extends App{
// Your code here!
// using => here for anofucntion
var anofun1 = (x:String, y:String, z: String) => x + " hello !! " + y + " " + z
// caling function
var result = anofun1("String one ", "String two ", "String three ")
//printing result
println("result for function is ")
println(result)
}
Output:
Example #2
In this example, we are using a wildcard to define our anonymous function.
Code:
object Main extends App{
// Your code here!
// using wildcard here for anofucntion
var anofun1 = (_:String) + (_:String) + "hello " + (_:String)
// caling function
var result = anofun1("String one ", "String two ", "no input !!")
//printing result here
println("result for function is ")
println(result)
}
Output:
Example #3
In this example, we are showing how to use without parameter anonymous function in scala.
Code:
object Main extends App{
// Your code here!
// using wildcard here for anofucntion
// here we are calling a anonymus function withutparameer
var anofun1 = () => {
println("This is withput parameter function in scala. ,..!!")
}
// printing result here.
println("result is :: ")
println(anofun1())
}
Output:
Conclusion
Anonymous functions provide us the lightweight syntax to define our function. We can define anonymous functions in two ways with or without parameters. Also, we have lambda function which is also anonymous function in scala they also provide us an optimized and readable way for developers.
Recommended Articles
We hope that this EDUCBA information on “Scala Anonymous Function” was beneficial to you. You can view EDUCBA’s recommended articles for more information.