Home »
Scala
Lambda Expression in Scala
By IncludeHelp Last updated : October 20, 2024
Scala Lambda Expression
A lambda expression is an expression that initializes its value using an anonymous function instead of using a variable or value.
Syntax
val lambda_expression = (variable : Type) => expression
Advantages and Characteristics of Lambda Expressions
These are useful when the function is used in the lambda expression is not complex and is a simple function that is to be used in one place.
Lambda expressions are faster and expressive that the regular function that is declared and defined in Scala.
The lambda expression is reusable in Scala i.e. you can use a lambda expression multiple times by iterating over a collection of data structure to perform operations on them. This is generally a transformation expression that changes the value of the input parameter and returns the change value to the calling line of code.
Example 1
Lambda expression can be called and used just like any other normal method in Scala.
object MyClass {
val square = (x:Int) => x*x
def main(args: Array[String]) : Unit= {
var x = 43;
printf("The square of " + x + " is " + square(x))
}
}
Output
The square of 43 is 1849
Example 2
Lambda expressions can also be applied to collections or data structures. For applying to collection the method map() is used. The map() is a high-ordered function in Scala using which we can transform values of the collection using the lambda expression.
object MyClass {
def main(args: Array[String]) : Unit= {
val values = List(12, 87, 53, 95, 81)
val square = values.map((x:Int) => x*x)
println("The list is : " + values)
printf("The square list is : ")
println(square)
}
}
Output
The list is : List(12, 87, 53, 95, 81)
The square list is : List(144, 7569, 2809, 9025, 6561)
With the same method, the programmer can use the lambda expression on more than one collections using the map() method.
Example 3
object MyClass
{
def main(args:Array[String]) : Unit=
{
val square1 = List(9,2,5,34,1)
val square2 = List(13,6,8 )
val func = (a:Int) => 4*a
val res1 = square1.map( func )
val res2 = square2.map( func )
println("The sides of the square :"+square1)
println("The area is : "+res1)
println("The sides of the square :"+square2)
println("The area is : "+res2)
}
}
Output
The sides of the square :List(9, 2, 5, 34, 1)
The area is : List(36, 8, 20, 136, 4)
The sides of the square :List(13, 6, 8)
The area is : List(52, 24, 32)
Example 4
The lambda expression in Scala programming language can also be used as a parameter to the method.
object MyClass
{
def area( x:Int, f:Int => Double) = f(x)
def main(args:Array[String]) : Unit=
{
val square = area(53, r => r * r)
println("The area of square is "+ square)
}
}
Output
The area of square is 2809.0
Example 5
object MyClass
{
def area( l:List[Int], f:Int => Double) = l.map(f)
def main(args:Array[String]) : Unit=
{
val square = area(List(12, 87, 45), s => s * s)
println("The area of all given squares is "+square)
}
}
Output
The area of all given squares is List(144.0, 7569.0, 2025.0)