Lambda Functions
- C
- C++
- C#
- Java
- Rust
- Go
- Javascript
- Typescript
- Python
// No Native Support.
// Use named functions and function pointers.
int add(int first, int second)
{
return first + second;
}
int (*operation)(int, int) = add;
int result = operation(10, 20);
// Since C++11
// ------------------------------------
// Definition
// ------------------------------------
auto functionName = []([parameters]) {
// ...
};
// ------------------------------------
// Declaration Example
// ------------------------------------
auto add = [](int first, int second) {
return first + second;
};
int result = add(10, 20);
// Capture by value
int factor {2};
auto multiply = [factor](int value) {
return value * factor;
};
// Capture by reference
auto increment = [&factor]() {
factor++;
};
// ------------------------------------
// Definition
// ------------------------------------
Func<int, int, int> functionName = ([parameters]) => {
// ...
};
// ------------------------------------
// Declaration Example
// ------------------------------------
Func<int, int, int> add = (first, second) => first + second;
Func<int, int, int> addBlock = (first, second) =>
{
return first + second;
};
Action<string> print = message => Console.WriteLine(message);
int result = add(10, 20);
// Since Java 8
// ------------------------------------
// Definition
// ------------------------------------
([parameters]) -> {
// ...
}
// ------------------------------------
// Declaration Example
// ------------------------------------
BinaryOperator<Integer> add = (first, second) -> first + second;
Consumer<String> print = message -> System.out.println(message);
Supplier<Integer> getNumber = () -> 10;
int result = add.apply(10, 20);
List<String> names = List.of("Ada", "Grace");
names.forEach(name -> System.out.println(name));
// Also Known as Closures
// ------------------------------------
// Definition
// ------------------------------------
let function_name = |[parameters]| {
// ...
};
// ------------------------------------
// Declaration Example
// ------------------------------------
let add = |first: i32, second: i32| first + second;
let add_block = |first: i32, second: i32| {
return first + second;
};
let result = add(10, 20);
// Closures can capture values from their environment.
let factor = 2;
let multiply = |value: i32| value * factor;
// Go has function literals.
// ------------------------------------
// Definition
// ------------------------------------
functionName := func([parameters]) [returnType] {
// ...
}
// ------------------------------------
// Declaration Example
// ------------------------------------
add := func(first int, second int) int {
return first + second
}
result := add(10, 20)
// Function literals can capture values from their environment.
factor := 2
multiply := func(value int) int {
return value * factor
}
// Also Known as Arrow Functions
// ------------------------------------
// Definition
// ------------------------------------
const functionName = ( [parameters] ) => {
// ...
}
// ------------------------------------
// Declaration Example
// ------------------------------------
const functionName = (x, y) => { /*...*/ } // Default
const functionName = (x, y) => { return x * y; }
const functionName = (x, y) => x * y; // Same as above. (Return and {} can be omitted for simple/small code)
const functionName = (x) => { /*...*/ } // one parameter
const functionName = x => { /*...*/ } // one parameter (the parentheses can be omitted)
const functionName = () => { /*...*/ } // no parameter
// ------------------------------------
// Named Parameters (Keyword Arguments)
// ------------------------------------
// No Native Support.
// Use object destructuring.
const printUser = ({ name, age }) => {
console.log(`${name}: ${age}`);
}
printUser({ name: "Ada", age: 36 });
// ------------------------------------
// Optional Parameters
// ------------------------------------
// No Native Support as syntax.
// Check for undefined or use a default value.
const printUser = (name, age) => {
if (age === undefined) {
// ...
}
}
// ------------------------------------
// Default Argument for Parameters
// ------------------------------------
const connect = (host, port = 443) => {
// ...
}
// ------------------------------------
// Variable Number of Arguments to a Function Parameters
// ------------------------------------
// Option 1 - rest parameters syntax (Also Known as Spread Operator)
const addAll = (...args) => {
let result = 0;
for (element of args) {
result += element
}
return result
}
console.log(addAll(1, 2, 3))
// Option 2 - Arguments Object
// "Arguments Object" does NOT work with Arrow Functions
More Info:
- https://medium.com/@charpeni/arrow-functions-in-class-properties-might-not-be-as-great-as-we-think-3b3551c440b1
- https://zendev.com/2018/10/01/javascript-arrow-functions-how-why-when.html
- https://stackoverflow.com/questions/31362292/how-to-use-arrow-functions-public-class-fields-as-class-methods
- https://stackoverflow.com/questions/51400605/javascript-child-class-method-not-overriding-parent-class-method
- https://stackoverflow.com/questions/45881670/should-i-write-methods-as-arrow-functions-in-angulars-class/45882417#45882417
- https://basarat.gitbook.io/typescript/future-javascript/arrow-functions#tip-arrow-functions-and-inheritance
// Also Known as Arrow Functions
// ------------------------------------
// Definition
// ------------------------------------
const functionName = ([parameters]): returnType => {
// ...
};
// ------------------------------------
// Declaration Example
// ------------------------------------
const add = (first: number, second: number): number => first + second;
const addBlock = (first: number, second: number): number => {
return first + second;
};
const print = (message: string): void => console.log(message);
const getNumber = (): number => 10;
const result = add(10, 20);
# ------------------------------------
# Definition
# ------------------------------------
function_name = lambda [parameters]: expression
# ------------------------------------
# Declaration Example
# ------------------------------------
add = lambda first, second: first + second
result = add(10, 20)
numbers = [1, 2, 3]
doubled = list(map(lambda value: value * 2, numbers))
# Prefer def for complex logic.
def add(first, second):
return first + second