Language - Lambda function

Model Funny

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.

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.

Example

Python: Sum of two arguments, a and b.

add = lambda a, b: a + b
print add(1,2)





Discover More
Card Puncher Data Processing
Python - lambda functions

in Python. See The expression below yields a function object. The unnamed object behaves like a function object defined with lambda generates a function and returns it, while def generates a...



Share this page:
Follow us:
Task Runner