procedural programming, functional programming

procedural programming, functional programming

Brother Feng's original process-oriented explanation:

The parameter input of the function is the food eaten by the function, and the return value of the function return is the result pulled out by the function. The process-oriented idea is to treat the execution of the program as a series of end-to-end functions. A function eats, pulls The output is eaten by another function, and the other function eats it and then continues to pull it for the next function to eat. . .

Process-oriented: mechanical thinking, pipelined programming


For example:
user login process: front-end receives and processes user requests - "passes user information to the logic layer, logical word processes user information - "writes user information into database to
verify user login process: database query/processes user information - "hands over to logic Layer, the logic layer processes user information - "User information is handed over to the front end, and the front end displays user information

 

 

Functional programming: http://egon09.blog.51cto.com/9161406/1842475

 

#no function
array=[0,1,2,3,4]
ret=[]
for i in array:
ret.append(i**2)
print(ret)
[0, 1, 4, 9, 16]

#If we have 10,000 lists, then you can only define the above logic as a function
def map_test(array):
ret=[]
for i in array:
ret.append(i**2)
return ret
print(map_test(range(5)))

 

#If our needs change, instead of squaring each element in the list, and adding 1 and subtracting one, then we can do this
lis=[2,4,6,10,8]
def add_num(x):
    return x+1
def fun(func,array):
    li=[]
    for i in lis:
        li.append(func(i))
    return li
print(fun(add_num,lis))
print(fun(lambda x:x+1,lis))

# can use anonymous functions
print(fun(lambda x:x+1,lis))

#The above is the function of the map function, the result obtained by map is an iterable object print(map(lambda x:x-1,range(5)))

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325253855&siteId=291194637