Updated April 20, 2023
Introduction to Scala Lambda
Scala lambda functions are anonymous functions. 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 any kind of operation on them. Also lambdas functions are very light weighted in syntax hence make the code more under stable.
Syntax:
Below is the syntax of the lambda expression in the scala given by the scala doc. We can also have one practice example of the syntax for better understanding.
val name = (name_variable:Dat_Type) =>Transformation_Expression
Example:
val result = (a:Int) => a + 10
In the above example, we are adding 10 the value of a here. But as you can see it are very less lines of code and more readable.
How does Lambda Expression Work in Scala?
Lambda functions are also known as functions literal in scala. This means we can pass this into any method and these function literal objects are called function value. They always return a new value does not modify the current one. We can use them very effectively with the collection data structure in scala. For this we will see one example to understand this better;
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 on a list of integer. 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 retain 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)
valresult =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 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 in inside the map only. Let’s have a simple syntax to show this see below;
Example:
object Main extends App{
// Your code here!
val list = List(100, 300, 500, 345, 789, 900)
valmyFun = (a:Int) => a + 10
valresult =list.map( myFun )
println("result is ::")
println(result)
}
In the above example, we are creating a list and defining a lambda function on the next line. Now we can use this lambda function again and again for different list objects. But for now, we are using it with only one object that we have defined is a list of integers and incrementing the value of an element by 10. In the next line, we are just printing out the result. So lambda functions also provide us the ability to reuse them.
Examples of Scala Lambda
Following are the examples are given below:
Example #1
In this example, we are modifying the list by define the lambda function.
Code:
object Main extends App{
// Your code here!
val list = List(100, 300, 500, 345, 789, 900)
println("Elements in list before lambdas function ::")
println(list)
//defining lambdas function here ...
valmyFun = (a:Int) => a + 10
// calling the function here ///
valresult =list.map( myFun )
println("result is ::")
println("Elements in list after lambdas function ::")
println(result)
}
Output:
Example #2
In this example, we are trying to modifying the list of string elements and adding a new value to each element present in the list by using the lambda function.
Code:
object Main extends App{
// Your code here!
val list = List("amit", "sumit", "amita", "vinita", "sourabh", "megha")
println("Elements in list before lambdas function ::")
println(list)
//defining lambdas function here ...
valmyFun = (a:String) => a + "*** *** >>"
// calling the function here ///
valresult =list.map( myFun )
println("result is ::")
println("Elements in list after lambdas function ::")
println(result)
}
Output:
Example #3
In this example, we are using a filter method to filter out the list of elements. But we are defining our own lambda function inside the filter() method, it will return us the list which contains elements divisible by 5 only.
Code:
object Main extends App{
// Your code here!
val list = List(100, 300, 500, 345, 789, 900)
println("Elements in list before lambdas function ::")
println(list)
//using anonymous function here
varresult =list.filter((a: Int) => a % 5 == 0)
println("result is ::")
println("Elements in list after lambdas function ::")
println(result)
}
Output:
Example #4
In this example, we are modifying multiple list objects by using map() function available in Scala.
Code:
object Main extends App{
// Your code here!
val list1 = List(100, 300, 500, 345, 789, 900)
val list2 = List(500, 900, 1500, 20, 45,80)
println("elements in list1 before lambdas function ::")
println(list1)
println("elements in list2 before lambdas function ::")
println(list2)
//using anonymous function here
var result1 = list1.map((a: Int)=> a * a)
var result2 = list2.map((a: Int)=> a * a)
println("result is ::")
println("elements in list1 after lambdas function ::")
println(result1)
println("elements in list2 after lambdas function ::")
println(result2)
}
Output:
Conclusion
Lambda functions are reusable and they also reduce the lines of code and make the syntax very light weighted. This makes the code very much readable, under stable, and convenient for the developers to write and understand. They are called as anonymous function and lambda functions are very much similar with the java lambda functions.
Recommended Articles
We hope that this EDUCBA information on “Scala Lambda” was beneficial to you. You can view EDUCBA’s recommended articles for more information.