Language - Lambda function

1 - About

Lambda are shorthand to create small, anonymous functions.

lambda functions are restricted to a single expression.

Lambda expressions return a function when evaluated.

It is generally possible to assign the function to a variable.

Lambda expressions are particularly useful when you need to pass a simple function into another function. In that case, the lambda expression generates a function that is bound to the parameter being passed into the function.

The function returned by lambda also automatically returns the value of its expression statement, which reduces the amount of code that needs to be written.

Since a lambda expression returns a function, it can be used anywhere an object is expected. For example, you can create a list of functions where each function in the list was generated by a lambda expression.

3 - Usage

An excellent use case for lambda expressions is functional programming. In functional programming, you will often pass functions to other functions as parameters, and lambda can be used to reduce the amount of code necessary and to make the code more readable.

4 - Example

Python: Sum of two arguments, a and b.

add = lambda a, b: a + b
print add(1,2)
code/function/lambda.txt · Last modified: 2018/03/27 15:35 by gerardnico