xxxxxxxxxx
A lambda expression is a short block
of code which takes in parameters
and returns a value. Lambda expressions
are similar to methods, but they do
not need a name and they can be
implemented right in the body of a method.
parameter -> expression
To use more than one parameter, wrap them in parentheses:
(parameter1, parameter2) -> expression
Example
Use a lamba expression in the ArrayList's
forEach() method to print every item in the list:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(5);
numbers.add(9);
numbers.add(8);
numbers.add(1);
numbers.forEach( (n) -> { System.out.println(n); } );
}
}
xxxxxxxxxx
# The lambda keyword in Python provides a
# shortcut for declaring small and
# anonymous functions:
add = lambda x, y: x + y
print(add(5, 3))
# Output
# 8
# You could declare the same add()
# function with the def keyword:
def add(x, y):
return x + y
print(add(5, 3))
# Output
# 8
# So what's the big fuss about?
# Lambdas are *function expressions*:
print((lambda x, y: x + y)(5, 3))
# Output
# 8
# • Lambda functions are single-expression
# functions that are not necessarily bound
# to a name (they can be anonymous).
# • Lambda functions can't use regular
# Python statements and always include an
# implicit `return` statement.
We have to specify function names while creating them. However, there is a special class of functions for which we do not need to specify function names.
Definition
A lambda is an anonymous function that returns some form of data.
Lambdas are defined using the lambda keyword. Since they return data, it is a good practice to assign them to a variable.
Syntax#
The following syntax is used for creating lambdas:
svg viewer
In the structure above, the parameters are optional.
Let’s try creating a few simple lambdas.
Below, we can find a lambda that triples the value of the parameter and returns this new value:
xxxxxxxxxx
triple = lambda num : num * 3 # Assigning the lambda to a variable
print(triple(10)) # Calling the lambda and giving it a parameter
xxxxxxxxxx
Facilitates functional programming - Lambda Expression facilitates functional programming and simplifies the development a lot.
To provide the implementation of the Java 8 Functional Interface.
Reduced Lines of Code - One of the clear benefits of using lambda expression is that the amount of code is reduced, we have already seen that how easily we can create instances of a functional interface using lambda expression rather than using an anonymous class.
Passing Behaviors into methods - Lambda Expressions enable you to encapsulate a single unit of behavior and pass it to other code.
For example, to other methods or constructors.
The evolution or timeline of Lambda looks something like this:
On-Prem DataCenter –> IAAS –> PaaS –> containerization/Docker –> Serverless
Lambda is a compute service where you can upload your code and create a Lambda function. AWS Lambda takes care of provisioning and managing the servers that you use to run the code. You do not have to worry about operating systems, patching, scaling etc.
It is essentially described as an event-driven compute service where AWS Lambda runs your code in response to events. These events could be changes to the data in an Amazon S3 bucket or an Amazon Dynamo DB table.
Lambda events can trigger other Lambda events or call other AWS services like SQS or SNS.
xxxxxxxxxx
Runnable subTaskWithLambda = () ->
{
System.out.println("SubTaskWithLambda started...");
};
Copied!