table of Contents
- Higher-order functions
- Mathematical concepts
- Built-in higher-order functions
- Currying
- Decorator
01 higher-order functions
1.1 mathematical concepts
y = g (f (x) ), in mathematics and computer science, higher-order functions should be at least a function satisfying the following conditions:
1) receiving one or more parameters as a function of
2) a function of the output
example of a counter:
def counter(base):
def inc(step=1):
nonlocal base
base += step
return base
return inc
1.2 built-in functions - higher-order functions
sorte (iterable [, key] [ , reverse]) sorting, returns a list of
filter (function, iterable) iterables filter element returns an iterator
map (function, * iterable) - > map object for a plurality of iterative element object maps as specified function returns an iterator
1.3 currying Currying
Currying: refers to the original function accepts two parameters into a new parameter receiving function, the function returns a new function to the existing second parameter is parameter
z = f (x, y) conversion as z = f (x) (y )
It can be converted into a function nested function by function currying
For example
def add(x,y):
return x+y
After the change
def add(x):
def _add(y):
nonlocalt x
retrun x+y
return _add
1.4 decorator:
Is introduced:
a summing function, thought to enhance its functionality, can output been called and the parameters of the call information
def add(x, y):
return x + y
Adding information output function:
def add(x, y):
print("call add, {}+{}".format(x,y))
return x + y
The above functions are completed addition the demand, but has the following disadvantages:
1) coupling high print statement
2) add function belongs to a service function, and outputs information pertaining to non-business function code, the function should not be placed in service in addition
The following improvements:
def add(x, y):
return x + y
def logger(fn):
print("Begin")
x = fn(4, 5)
print("end")
print(logger(add))
Then separating business functions do not intrusive codes
improved
def add(x,y):
return x+y
def logger(fn,x,y):
print('before')
ret = fn(x,y)
print('after')
return ret
But the fn parameter is restricted only for containing two parameters
continue to improve
def add(x,y):
return x+y
def logger(fn,*args,**kwargs):
print('before')
ret = fn(*args,**kwargs)
print('after')
return ret
Currying way
def add(x,y):
return x+y
def logger(fn):
def _logger(*args, **kwargs):
print('before')
ret = fn(*args, **kwargs)
print('after')
return ret
return _logger
# 如下调用
add = logger(add) # 因为函数调用的关系,参数add的引用用在函数内部保存
add(4, 5) # 调用的是内部保存的add引用,而非原本的add函数
The improvement as Python decorators
def logger(fn):
def _logger(*args, **kwargs):
print('before')
ret = fn(*args, **kwargs)
print('after')
return ret
return _logger
@logger
def add(x,y):
return x+y
print(add(4, 10))
- Decorators and higher-order functions
decorators are higher-order functions, but the decoration is functional or decorative function passed into the function enhancement