What is the use of LAMBDA expressions? How do I use it?

Source: Internet
Author: User
In short, the lambda expression mentioned in programming is usually used in situations where a function is required but does not bother to name a function, that is, an anonymous function. The relationship between this usage and the so-called lambda calculus (the wiki link in the title description) is somewhat like the relationship between the atom bomb and the mass energy equation, which is actually quite large.

We don't talk about the formal lambda calculus, only the anonymous function that has a practical purpose. Let's start with a common Python example: square each element in a list:

Map (Lambda x:x*x, [y for y in range (10)])


It's better than that.

def sq (x):    return x * xmap (sq, [y for y in range (10)])

, because the latter defines a (polluting) function, especially if the function is only used once. And the first one is actually easier to read, because the function that maps to the list is exactly what you want to do, and it's very straightforward. If you look closely at your code, you will find that this scenario is quite common: you really need a function in a place where you can do one thing, and it doesn't matter what name it's called. Lambda expressions can be used to do this.

Further, the anonymous function is essentially a function, and what it abstracts out is a set of operations. What does that mean? Analogy
A = [1, 2, 3]
And

F = Lambda x:x + 1


, you will find that the right side of the equal sign can be completely separated from the left side of the equal sign, and the name on the left side of the equal sign is just the identifier of the entity on the right. If you can get used to [1, 2, 3] alone, then lambda x:x + 1 can also exist alone in fact it is not difficult to understand, its meaning is to "one number plus one" the operation itself.

Now look back at the map () function, which can map a function above an enumerable type. Use the A and F given above to write:
Map (f, a)
That is, the function f is applied to each element of a in turn, to obtain the result [2, 3, 4]. Now replace F with a lambda expression, it becomes:
Map (Lambda x:x + 1, [1, 2, 3])
Do you think it's clear at the moment? Especially the analogy.

A = [1, 2, 3]r = []for Each in a:    r.append (each+1)

This way, you will find yourself if you can "iterate through the list, to do some of the elements encountered in the process of abstraction from a loop to become a function map, and then use the lambda expression as a parameter to the map, consider the matter of thinking level will be higher, There is less detail to take into account. Python, like "advanced" functions that use lambda expressions, reduce, filter, and so on, are available in many languages (though these features are best not to be used in Python too much.) This function, which accepts a function as a parameter, is called a "higher order function" (Higher-order functions) and is derived from the idea of functional programming (functional programming).

Python has more lambda limitations than many other languages, and the worst is that it can only be made up of one expression. This limitation is mainly to prevent abuse, because when people find that lambda is very convenient, it is easier to abuse, but more can make the program look less clear, after all, everyone's tolerance/understanding of the level of abstraction is different.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.