What is the use of Lambda expressions? How to use it?

Source: Internet
Author: User
Lambda expressions are available in Python, C ++, and other languages. To put it simply, the lambda expressions mentioned in programming are usually used when a function is needed, but you don't want to bother naming a function, that is, an anonymous function. The relationship between this usage and the so-called Lambda algorithm (the Wikipedia link in the question description) is a bit like the relationship between the atomic bomb and the energy equation. The difference is actually quite big.

Aside from formal Lambda computations, only anonymous functions with actual purposes are involved. Let's take a common Python example: Square each element in a list:

map( lambda x: x*x, [y for y in range(10)] )


This method is better

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

Because the latter defines one more (polluting the environment) function, especially if this function will only be used once. In addition, the first method is actually easier to read, because the function mapped to the list is very clear about what to do. If you carefully observe your code, you will find that this scenario is very common: You actually only need a function that can do one thing at a time, it doesn't matter what its name is. Lambda expressions can be used to do this.

Further, an anonymous function is essentially a function. What it abstracts is a set of operations. What does this mean? Analogy
A = [1, 2, 3]
And

f = lambda x : x + 1


, You will find that the thing on the right of the equal sign can completely exists from the thing on the left of the equal sign. The name on the left of the equal sign is only the identifier of the object on the right. If [1, 2, 3] exists independently, lambda x: x + 1 can also exist independently, it means adding a number to the computing itself.

Now let's look back at the map () function, which can map a function to an enumerated type. Follow the and f given above, you can write:
Map (f,)
That is, the function f is applied to each element of a in sequence to obtain the results [2, 3, 4]. Now, replace f with a lambda expression and it becomes:
Map (lambda x: x + 1, [1, 2, 3])
Do you think it is clear now? Especially analogy

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

In this way, you will find that if you can abstract the process of "traversing the list and performing some operation on each element encountered" from a loop into a function map, then, if lambda expressions are used to pass this operation as a parameter to map, the thought level of things will be higher, and the details to be taken into account will be less. In Python, similar to the "advanced" functions that can use lambda expressions, as well as reduce and filter, many languages also have such tools (but these features should not be used too much in Python. This kind of function that can accept a function as a parameter is called the higher-order function. It is a function programming idea.

Compared with many other languages, Python has a lot of lambda restrictions. The most serious one is that it can only consist of one expression. This restriction is mainly used to prevent abuse, because it is easy to abuse lambda when it is found convenient, but when it is used too much, it will make the program look less clear, after all, everyone's patience/understanding of the abstract level 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.