Introduction :
Lambda expression is an anonymous function that uses a variable instead of a value. It allows us to create functions that are easily reusable.
Syntax:
val Variable Name = (variable:Type) => Transformation_Expression
Example:
val square = (x:Int) => x * x
How to write Lambda Expression in Scala :
Lambda expressions in Scala the syntax for these uses a symbol it’s an equals and a greater than and we refer to this as rocket ( => ). When we’re reading the code the idea of a lambda expression is a short literal expression that defines a function and typically these should not be overly long. so for example I could define a function for squaring values that looks something like this.
val Square = x => x * x
Now before I’m going to write this first, Scala is going to be unhappy with it okay so the rocket here is basically showing us we have arguments on the left side of the rocket symbol and it is going to some expression over on the other side and so X goes to x squared.
You can though technically this is a method of the object HelloWorld. It’s not really a function.
For Example :
Object HelloWorld{
def Square(x:Double):Double = x*x
}
There are lots of places in Scala where we are going to write functions that we’re actually not going to give names. They’re just going to be something that’s small. We use them once we don’t need to use them multiple times and writing exist for them is a little bit heavy-handed. This turns out the methods in the places that want to have functions passed in, so the distinction between them isn’t all that strong.
val Square = x => x * x
What is them, Scala tells about here the problem is type it needs to know what this input argument type is and so in order to make this correct one way we could do that is to put a type on this and put parentheses around this so that the argument you know to enclose this.
val Square = (x:Double) => x*x
println(Square(5))
We can also write lambda expression like that:
val Square:Double => Double = x => x*x
Example of Lambda Expressions :
// Creating object
object MathematicalOperations
{
// Main method
def main(args:Array[String])
{
// lambda expression
val multiply = (x:Int, y:Int) => x * y
println(multiply(2, 3))
}
}
Output: 6
Lambda Expression with map() :
In order to transform the elements of a collection, we generally use the map() function. It is a higher-order function that takes a lambda expression as a parameter. The function transforms the elements of the collection according to its definition.
// Creating object
object SquareTheList
{
// Main method
def main(args:Array[String])
{
// list of numbers
val ListOfNumbers = List(1,2,3,4,5)
// squaring each element of the list
val result = ListOfNumbers.map( (x:Int) => x * x )
println(result)
}
}
Output: List(1,4,9,16,25)
References:
https://www.geeksforgeeks.org/lambda-expression-in-scala/
Conclusion:
This topic will be used in plenty of useful places in your application. Don’t put the entire code in every place rather define the above method and use it everywhere. Make it a practice for all exceptions which a small of code may throw at runtime. Also, try to include a possible course of action, the user should follow in case these exceptions occur.
That’s all basic, I have in my mind as of now related to exception handling best practices, which could be understood by any beginner level programmer. The language does not matter. You can write in any language you want to write. If you are working on python, Except is used in place of Catch. So, focus on the logic. If you found anything missing or you do not relate to my view on any point, drop me a comment. I will be happy to discuss. For more blogs, click here
Happy Learning !!
1 thought on “Lambda Expressions in Scala3 min read”
Comments are closed.