Functional programming is the use of a series of functions to solve the problem, according to the general programming thinking, the face of the problem we think the way is "how to", and functional functional programming thinking is what I want to "do." As for the features of functional programming are not summarized, we directly take examples to understand what is functional programming.
Lambda expression (anonymous function):
How normal functions and anonymous functions are defined:
Copy Code code as follows:
#普通函数
def add (a,b):
Return a + b
Print Add (2,3)
#匿名函数
Add = Lambda A,b:a + b
Print Add (2,3)
#======== Output ===========
5
The naming rule for anonymous functions, identified by the LAMDBA keyword, the left side of the colon (:) represents the parameter received by the function (A,b), and the right side of the colon (:) represents the return value of the function (A+B).
Because LAMDBA does not need to be named when it is created, it is called an anonymous function ^_^
map function: Calculating string length
Copy Code code as follows:
ABC = [' com ', ' fnng ', ' cnblogs ']
For I in range (LEN (ABC)):
Print Len (Abc[i])
#======== Output ===========
4
Defines an array of ABC strings, calculates the ABC length, and then loops out the length of each string in the array.
Take a look at how the map () function implements this process.
Copy Code code as follows:
Abc_len = map (len,[' Hao ', ' fnng ', ' cnblogs ')]
Print Abc_len
#======== Output ===========
[3, 4, 7]
Although the results of the output are the same, but their form is different, the first is a pure numeric value, the map () function output still retains the format of the array.
Uppercase and lowercase conversions;
Python is provided with, upper () and lower () to convert case.
Copy Code code as follows:
#大小写转换
ss= ' Hello world! '
Print Ss.upper () #转换成大写
Print Ss.lower () #转换成小写
#======== Output ===========
HELLO world!
Hello world!
Transformation through the map () function:
Copy Code code as follows:
def to_lower (item):
Return Item.lower ()
Name = Map (to_lower,[' cOm ', ' fnng ', ' cnblogs '])
Print Name
#======== Output ===========
[' com ', ' fnng ', ' cnblogs ']
As we can see in this example, we write a function toupper, which does not change the values that are passed in, but simply makes the incoming value a simple operation and then returns. Then we use it in the map function and we can clearly describe what we want to do.
Let's see how the normal way is to implement string capitalization:
Copy Code code as follows:
ABC = [' COm ', ' fnng ', ' cnblogs ']
Lowname = []
For I in range (LEN (ABC)):
Lowname.append (Abc[i].lower ())
Print Lowname
#======== Output ===========
[' Hao ', ' fnng ', ' cnblogs ']
The map () function, coupled with lambda expressions (anonymous functions), can achieve more powerful functionality.
Copy Code code as follows:
#求平方
#0 *0,1*1,2*2,3*3,.... 8*8
Squares = map (lambda x:x*x, Range (9))
Print Squares
#======== Output ===========
[0, 1, 4, 9, 16, 25, 36, 49, 64]
Reduce function:
Copy Code code as follows:
def add (a,b):
Return a+b
Add = Reduce (add,[2,3,4])
Print add
#======== Output ===========
For the reduce function, which requires two data to be processed each time, the first selection of 2 and 3 is added after adding the Add function to get 5, followed by 5 and 4, and then by the Add function, which finally gets 9.
As we can see in the previous Map function example, the map function is to process only one data at a time.
Then we found how simple it was to use the reduce function to add a lambda expression to the implementation of factorial:
Copy Code code as follows:
#5阶乘
#5! =1*2*3*4*5
Print reduce (lambda x,y:x*y, Range (1,6))
#======== Output ===========
In addition to map and reduce in Python, there are other functions, such as filter, find, all, and any, that are auxiliary (other functional languages are available) to make your code simpler and easier to read. Let's look at a more complicated example:
Copy Code code as follows:
#计算数组中正整数的值
Number =[2,-5, 9,-7, 2, 5, 4,-1, 0,-3, 8]
Count = 0
sum = 0
For I in range (len (number)):
If number[i]>0:
Count + 1
Sum + + Number[i]
Print Sum,count
If count>0:
Average = Sum/count
Print average
#======== Output ===========
6
If you use functional programming, this example can be written like this:
Copy Code code as follows:
Number =[2,-5, 9,-7, 2, 5, 4,-1, 0,-3, 8]
sum = filter (lambda x:x>0, number)
Average = reduce (lambda x,y:x+y, sum)/len (sum)
Print average
#======== Output ===========
Finally, we can see that functional programming has the following benefits:
The
1) code is simpler.
2) DataSet, operations, return values are put together.
3) When you read the code, there is no loop body, so you can lose some of the temporary variables and the variables fall back to the logic.
4) Your code becomes a description of what you are going to do rather than how to do it.