Lambda Expression refers to an expression that uses an anonymous function instead of variable or value. Lambda expressions are more convenient when we have a simple function to be used in one place. These expressions are faster and more expressive than defining a whole function. We can make our lambda expressions reusable for any kind of transformations. It can iterate over a collection of objects and perform some kind of transformation to them.
Syntax:
val lambda_exp = (variable:Type) => Transformation_Expression
Example:
// lambda expression to find double of x val ex = (x:Int) => x + x
- We can pass values to a lambda just like a normal function call.
Example :// Scala program to show// working of lambda expression// Creating objectobjectGfG{// Main methoddefmain(args:Array[String]){// lambda expressionvalex1=(x:Int)=>x +2// with multiple parametersvalex2=(x:Int, y:Int)=>x * yprintln(ex1(7))println(ex2(2,3))}}chevron_rightfilter_noneOutput:
9 6
- To apply transformation to any collection, we generally use map() function. It is a higher-order function where we can pass our lambda as a parameter in order to transform every element of the collection according to the definition of our lambda expression.
Example :
// Scala program to apply// transformation on collection// Creating objectobjectGfG{// Main methoddefmain(args:Array[String]){// list of numbersvall=List(1,1,2,3,5,8)// squaring each element of the listvalres=l.map( (x:Int)=>x * x )/* ORval res = l.map( x=> x * x )*/println(res)}}chevron_rightfilter_noneOutput:
List(1, 1, 4, 9, 25, 64)
We can see that the defined anonymous function to perform the square operation is not reusable.
- We are passing it as an argument. However, we can make it reusable and may use it with different collections.
Example :// Scala program to apply// transformation on collection// Creating objectobjectGfG{// Main methoddefmain(args:Array[String]){// list of numbersvall1=List(1,1,2,3,5,8)vall2=List(13,21,34)// reusable lambdavalfunc=(x:Int)=>x * x// squaring each element of the listsvalres1=l1.map( func )valres2=l2.map( func )println(res1)println(res2)}}chevron_rightfilter_noneOutput:
List(1, 1, 4, 9, 25, 64) List(169, 441, 1156)
- A lambda can also be used as a parameter to a function.
Example :// Scala program to pass lambda// as parameter to a function// Creating objectobjectGfG{// transform function with integer x and// function f as parameter// f accepts Int and returns Doubledeftransform( x:Int, f:Int=>Double)=f(x)// Main methoddefmain(args:Array[String]){// lambda is passed to f:Int => Doublevalres=transform(2, r=>3.14* r * r)println(res)}}chevron_rightfilter_noneOutput:
12.56
In above example, transform function accepts integer x and function f, applies the transformation to x defined by f. Lambda passed as the parameter in function call returns Double type. Therefore, parameter f must obey the lambda definition.
- We can perform the same task on any collection as well. In case of collections, the only change we need to make in transform function is using map function to apply transformation defined by f to every element of the list l.
Example :// Scala program to pass lambda// as parameter to a function// Creating objectobjectGfG{// transform function with integer list l and// function f as parameter// f accepts Int and returns Doubledeftransform( l:List[Int], f:Int=>Double)=l.map(f)// Main methoddefmain(args:Array[String]){// lambda is passed to f:Int => Doublevalres=transform(List(1,2,3), r=>3.14* r * r)println(res)}}chevron_rightfilter_noneOutput:
List(3.14, 12.56, 28.259999999999998)
Recommended Posts:
- Overuse of lambda expressions in Python
- Lambda Function - Ruby
- Scala Tutorial – Learn Scala with Step By Step Guide
- Perl | Backtracking in Regular Expression
- Perl | Quantifiers in Regular Expression
- JavaScript | class expression
- Perl | Operators in Regular Expression
- Difference between function expression vs declaration in JavaScript
- Kotlin Regular Expression
- JavaScript | function* expression
- JavaScript | async function expression
- Convert user input string into regular expression using JavaScript
- Scala short <(x: Short): Boolean
- Scala short <(x: Char): Boolean
- Scala Extractors
- Scala | Partially Applied functions
- Scala String indexOf(String str) method with example
- Scala String contentEquals() method with example
- Scala Keywords
- Scala Int /(x: Int) method with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

