Lambda function — What’s it and Why we use it?
Lambda function is also named Anonymous functions. It originate in the work of Alonzo Church in his invention of the lambda calculus in 1936, before electronic computers, in which all functions are anonymous.
Nowadays, Lambda functions are ubiquitous in functional programming languages and other languages with first-class functions which allow functions to be assigned to variables, passed as arguments to other functions, and returned as the values from other functions.(Such as Python, Go, Haskell)
Lambda function consists of 3 parts “parameters -> expression”. For example (x,y)->x+y means accept two parameters and return their sum. Just like its name “Anonymous”, you don’t need to give a name to it. In Python, we can write an add function in form of
In Python, you can give any number of parameters (including of optional parameters) to Lambda function and it will return a function of a single expression value. (Attention: we can not write an instruction (if,for .etc) in it)
In brief, Lambda function is very simple and light. So when the function is only used once, or a limited number of times, we often use an anonymous function instead of using a named function.
THREE typical cases of using Lambda function
1、Functional programming
For example, a sorting function like sorted() usually accepts an arbitrary function that determines how to compare two elements, so here we use Lambda function as the sorting logic.
2、 Lambda functions are often arguments being passed to higher-order functions
A higher-order function is a function that takes one or more functions as arguments or returns a function as its result.
For example, map, filter and reduce are 3 higher-order functions that often used in Python.
The map function performs a function call on each element of a list.
The filter function returns all elements from a list that evaluate True when passed to a certain function.
The reduce function runs over all elements in a list (usually left-to-right), accumulating a value as it goes.
3、Closures
Closures allow a function that can access the captured variables of other functions even when the function is invoked outside their scope.
In the example, Lambda function is a closure so that in the local scope of add, the value of n can be accessed in the global scope because of closures. So add1(15) can be executed and return 20.
Conclusion
Lambda function has a very simple and understandable style. It is never the only way to solve a problem. Usually we use it because with Lambda function, we can encapsulate specific, non-reusable code with only some little one-line normal functions instead of writing a long named-function. But never use too many Lambda functions in programming because it will make your code too difficult to understand.
What’s more, Lambda functions are often used as the argument of higher-order functions like map, filter .etc or as callback to be passed to some functions. It will be more efficient and have less errors than calling a named-function.