What are Lambda Expressions or anonymous function in different programming languages?

Lambda Expressions in C++

In C++11 and later, a lambda expression — often called a lambda — is a convenient way of defining an anonymous function object (a closure) right at the location where it is invoked or passed as an argument to a function. Typically lambdas are used to encapsulate a few lines of code that are passed to algorithms or asynchronous methods.

#include <algorithm>
#include <cmath>
void abssort(float* x, unsigned n) {
std::sort(x, x + n,
// Lambda expression begins
[](float a, float b) {
return (std::abs(a) < std::abs(b));
} // end of lambda expression
);
}

Lambda Expressions in Python 🐍

Small anonymous functions can be created with the lambda keyword. This function returns the sum of its two arguments: lambda a, b: a+b. Lambda functions can be used wherever function objects are required. They are syntactically restricted to a single expression. Semantically, they are just syntactic sugar for a normal function definition. Like nested function definitions, lambda functions can reference variables from the containing scope.

def make_incrementor(n):
return lambda x: x + n

f = make_incrementor(5)
f(0)

f(1)
Python lambda function example

Lambda Expressions—Java 8

Lambda expressions are introduced in Java 8 and are touted to be the biggest feature of Java 8. Lambda expression facilitates functional programming and simplifies the development a lot. I will talk about functional programming in another post.if you want you can read it about in Wikipedia.

Syntax

A lambda expression is characterized by the following syntax.

parameter -> expression body
public class Java8Lambda {public static void main(String args[]) {
Java8Lambda tester = new Java8Lambda();

//with type declaration
MathOperation addition = (int a, int b) -> a + b;

//with out type declaration
MathOperation subtraction = (a, b) -> a - b;

//with return statement along with curly braces
MathOperation multiplication = (int a, int b) -> { return a * b; };

//without return statement and without curly braces
MathOperation division = (int a, int b) -> a / b;

System.out.println("10 + 5 = " + tester.operate(10, 5, addition));
System.out.println("10 - 5 = " + tester.operate(10, 5, subtraction));
System.out.println("10 x 5 = " + tester.operate(10, 5, multiplication));
System.out.println("10 / 5 = " + tester.operate(10, 5, division));

//without parenthesis
GreetingService greetService1 = message ->
System.out.println("Hello " + message);

//with parenthesis
GreetingService greetService2 = (message) ->
System.out.println("Hello " + message);

greetService1.sayMessage("Rajdeep");
greetService2.sayMessage("Sougata");
}

interface MathOperation {
int operation(int a, int b);
}

interface GreetingService {
void sayMessage(String message);
}

private int operate(int a, int b, MathOperation mathOperation) {
return mathOperation.operation(a, b);
}
}
Java 8 lambda demo

Arrow functions in JavaScript ES6

An arrow function is a modern way to declare the anonymous function. ES6 fat arrow functions have a shorter syntax compared to function expressions and lexically bind the thisvalue. Arrow functions are always anonymous and effectively turn function (arguments) { expression }into arguments => expression. If using an expression after an arrow, the return is implicit, so no return is required.

// Parenthesis are optional depending on the number of arguments:
let square = x => x * x;
console.log(square(10));

let add = (a, b) => a + b;
console.log(add(3, 4));
Demo javascript es6 arrow function.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rajdeep Das

Rajdeep Das

Passion about Computer Science,Software Engineering. I help brands and Startups with software development.