- Hands-On Data Science and Python Machine Learning
- Frank Kane
- 356字
- 2021-07-15 17:15:03
Lambda functions - functional programming
One more thing that's kind of a Python-ish sort of a thing to do, which you might not see in other languages is the concept of lambda functions, and it's kind of called functional programming. The idea is that you can include a simple function into a function. This makes the most sense with an example:
#Lambda functions let you inline simple functions
print (DoSomething(lambda x: x * x * x, 3))
The output of the above code is as follows:
27
We'll print DoSomething, and remember that our first parameter is a function, so instead of passing in a named function, I can declare this function inline using the lambda keyword. Lambda basically means that I'm defining an unnamed function that just exists for now. It's transitory, and it takes a parameter x. In the syntax here, lambda means I'm defining an inline function of some sort, followed by its parameter list. It has a single parameter, x, and the colon, followed by what that function actually does. I'll take the x parameter and multiply it by itself three times to basically get the cube of a parameter.
In this example, DoSomething will pass in this lambda function as the first parameter, which computes the cube of x and the 3 parameter. So what's this really doing under the hood? This lambda function is a function of itself that gets passed into the f in DoSomething in the previous example, and x here is going to be 3. This will return f of x, which will end up executing our lambda function on the value 3. So that 3 goes into our x parameter, and our lambda function transforms that into 3 times 3 times 3, which is, of course, 27.
Now this comes up a lot when we start doing MapReduce and Spark and things like that. So if we'll be dealing with Hadoop sorts of technologies later on, this is a very important concept to understand. Again, I encourage you to take a moment to let that sink in and understand what's going on there if you need to.
- Flutter開發(fā)實(shí)戰(zhàn)詳解
- Java Web開發(fā)技術(shù)教程
- Oracle Database 12c Security Cookbook
- 精通Python自然語言處理
- Access 2016數(shù)據(jù)庫管
- Visual C#通用范例開發(fā)金典
- HTML5 APP開發(fā)從入門到精通(微課精編版)
- Instant Automapper
- Python Web自動(dòng)化測試設(shè)計(jì)與實(shí)現(xiàn)
- 新手學(xué)ASP.NET 3.5網(wǎng)絡(luò)開發(fā)
- C++ Primer(中文版)(第5版)
- WordPress Responsive Theme Design
- Getting Started with SpriteKit
- Python機(jī)器學(xué)習(xí)(原書第3版)
- Magento 2 Development Essentials