
Introduction to Scala Anonymous Function
A function that does not contain any name or a function without a name is called an anonymous function in scala. This anonymous function is also called a function literal. Inside the anonymous function, we can write our logic and define our function. We can create a very lightweight function also if we can create an inline function.
Syntax
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?
Anonymous functions are also called function literals and these functions are literal and their object is known as the function value. While working with the scala anonymous function we use the 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 our parameters. Therefore 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 character represents the parameter that appears only once.
syntax:
(_:Double)*(Double)
2) using ‘=>’: This is the most common way to define the anonymous function. This arrow we can say acts as a callback here which is also responsible to transmit the parameter to the logic and evaluating them and provide us the result.
syntax:
(a: String) => a + "Hello !!"
Anonymous functions are of two types which are as follow;
1) Anonymous function without a parameter: We can define our anonymous function without a 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.
Use Case
use case 1: Anonymous Function without parameters
// Scala program to illustrate the anonymous method
object prwatech{
def main(args: Array[String])
// Creating anonymous functions without parameter
var myfun1 = () => {"Welcome to Knoldus!"}
println(myfun1())
// A function which contain anonymous function as a parameter
def myfunction(fun:(String, String)=> String) =
{
fun("Akka", " Cluster")
}
// Explicit type declaration of anonymous function in another
function
val f1 = myfunction((str1: String,
str2: String) => str1 + str2)
// Shorthand declaration using wildcard
val f2 = myfunction(_ + _)
println(f1)
println(f2)
}
}
output:
Welcome to Knoldus!
Akka Cluster
Akka Cluster
use case 2: Anonymous function with parameter
// Scala program to illustrate the anonymous method
object prwatech
{
def main(args: Array[String])
// Creating anonymous functions with multiple parameters
Assign
// anonymous functions to variables
var myfunction1 = (str1:String, str2:String) => str1 + str2
// An anonymous function is created
var myfunction2 = (_:String) + (_:String)
// Here, the variable invoke like a function call
println(myfunction1("Scala/Akka", " Knoldus"))
println(myfunction2("Spark", " Knoldus"))
}
}
output
Scala/Akka Knoldus
SparkKnoldus
Conclusion
Anonymous functions provide us the lightweight syntax to define our function. In Addition We can define anonymous functions in two ways with or without parameters. Also, we have lambda function which is also an anonymous function in scala they also provide us an optimized and readable way for developers.