To pass functions as arguments, such functions are called higher-order functions, and functional programming refers to this highly abstract programming paradigm.
Lambda
(Lambda
Adorner decorator
The adorner is a well-known design pattern, which is often used for scenes with cut-off requirements, with more classic insert logs, performance tests, transaction processing, web rights checks, caches, and so on. For example logging, some functions need to be recorded. Stupid way, each function to add code, if the code changed, it is sad. Decorator's approach, define a special logging adorner, decorate the required function, and fix it.
Pros: Draw out the same code that is not relevant to the function itself in a large number of functions and continue to reuse. That is, the function can be "decorated" as a completely different behavior, effectively orthogonal decomposition of business logic, such as the use of permissions and authentication from the business independent.
In summary, the function of an adorner is to add additional functionality to an already existing object.
Essentially, decorator is a higher-order function that returns a function
deflog (func):defWrapper (*args, * *kw):Print 'Call %s ():'% func.__name__ returnFunc (*args, * *kw)returnwrapper @log#equals now = log (now)defNow ():Print '2013-12-25'------------------------------>>>Now () Call Now ():2013-12-25#if the decorator itself needs to pass in parameters, it is necessary to write a higher-order function that returns decorator, which is more complex to write. For example, to customize the text of a log:deflog (text):defDecorator (func):defWrapper (*args, * *kw):Print '%s%s ():'% (text, func.)__name__) returnFunc (*args, * *kw)returnwrapperreturnDecorator @log ('Execute')#equals now = log (' Execute ') (now)defNow ():Print '2013-12-25'------------------------------>>>Now () execute now ()
There are some details to note. After the decoration (function properties such as the name of the functions will change). So when writing an adorner, it is best to add the warp of the Functools module before implementation, which preserves the name and docstring of the original function.
ImportFunctoolsdeflog (func): @functools. Wraps (func)defWrapper (*args, * *kw):Print 'Call %s ():'% func.__name__ returnFunc (*args, * *kw)returnwrapper @logdefNow ():Print '2013-12-25'
Python built-in map, reduce, filter, usage of the sorted function
Map (f, [X1, x2, X3, x4]) = [F (x1), F (x2), F (x3), F (x4)] Map (LambdaX,y:x+y, [1,1,1], [2,3,4]) reduce (f, [X1, x2, X3, X4])=F (f (f (x1, x2), x3), X4) filter (f, [1, 2, 4, 5, 6, 9, 10, 15])#filter data that conforms to F and returns the listSorted ([x1, x2, X3, X4], F)#sort in the form F, return to listdefStr2Int (s):deffn (x, y):returnX * 10 +ydefChar2num (s):return{'0': 0,'1': 1,'2': 2,'3': 3,'4': 4,'5': 5,'6': 6,'7': 7,'8': 8,'9'8 {}[s]returnreduce (FN, map (Char2num, s))
#further simplifying with lambda functionsdefChar2num (s):return{'0': 0,'1': 1,'2': 2,'3': 3,'4': 4,'5': 5,'6': 6,'7': 7,'8': 8,'9'8 {}[s]defStr2Int (s):returnReduceLambdaX,y:x*10+y, Map (Char2num, s))
2015-05-02
Functional programming of Python