Incoming function
To understand that the function itself can also be passed in as a parameter, you can start with the Map/reduce function built into Python.
If you read Google's famous paper "Mapreduce:simplified Data processing on Large Clusters", you can probably understand the concept of map/reduce.
Let's look at map first. The map()
function receives two parameters, one is a function, the other is a sequence, the map
incoming function functions sequentially to each element of the sequence, and returns the result as a new list.
For example, we have a function f (x) =x2, to function on a list [1, 2, 3, 4, 5, 6, 7, 8, 9]
, it can be map()
implemented as follows:
Now, we use Python code to implement:
>>> def f(x):... return x * x...>>> map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])[1, 4, 9, 16, 25, 36, 49, 64, 81]
Notice the function we define f
. When we write f
, it refers to the function object itself, when we write f(1)
, referring to the call F function, and passed in Parameter 1, expecting to return the result 1.
Therefore, map()
the first parameter passed in is the f
function object itself.
Functions such as functions that map()
can receive functions as arguments are called higher-order functions (Higher-order function).
You might think that you don't need map()
a function, write a loop, or you can calculate the result:
L = []for n in [1, 2, 3, 4, 5, 6, 7, 8, 9]: L.append(f(n))print L
Yes, but, from the loop code above, can you see "putting F (x) in every element of the list and generating a new list"?
So, map()
as a higher-order function, in fact it abstracts the arithmetic rules, so we can calculate not only the simple f (x) =x2, but also any complex function.
Let's look at the usage of reduce. Reduce functions a function in a sequence [X1, x2, x3 ...] , the function must receive two parameters, and reduce calculates the result and the next element of the sequence, and the effect is:
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
For example, to sum a sequence, it can be implemented with reduce:
>>> def add(x, y):... return x + y...>>> reduce(add, [1, 3, 5, 7, 9])25
Of course, the sum operation can be built directly into Python sum()
, and no need to use reduce.
But if you want to [1, 3, 5, 7, 9]
transform the sequence into an integer 13579,reduce it will come in handy:
>>> def fn(x, y):... return x * 10 + y...>>> reduce(fn, [1, 3, 5, 7, 9])13579
This example is not very useful in itself, but if we consider that the string str
is also a sequence, with a slight change to the above example, map()
we can write the str
converted int
function:
>>> def fn(x, y):... return x * 10 + y...>>> def char2num(s):... return {‘0‘: 0, ‘1‘: 1, ‘2‘: 2, ‘3‘: 3, ‘4‘: 4, ‘5‘: 5, ‘6‘: 6, ‘7‘: 7, ‘8‘: 8, ‘9‘: 9}[s]...>>> reduce(fn, map(char2num, ‘13579‘))13579
str2int
the function that is organized into one is:
def str2int(s): def fn(x, y): return x * 10 + y def char2num(s): return {‘0‘: 0, ‘1‘: 1, ‘2‘: 2, ‘3‘: 3, ‘4‘: 4, ‘5‘: 5, ‘6‘: 6, ‘7‘: 7, ‘8‘: 8, ‘9‘: 9}[s] return reduce(fn, map(char2num, s))
You can also use lambda functions to further simplify:
def char2num(s): return {‘0‘: 0, ‘1‘: 1, ‘2‘: 2, ‘3‘: 3, ‘4‘: 4, ‘5‘: 5, ‘6‘: 6, ‘7‘: 7, ‘8‘: 8, ‘9‘: 9}[s]def str2int(s): return reduce(lambda x,y: x*10+y, map(char2num, s))
That is, assuming that Python does not provide a int()
function, you can write a function that converts the string to an integer by itself, and only requires a few lines of code!
The use of lambda functions is described in the next section.
Sorting algorithms
Sorting is also an algorithm that is often used in programs. Whether you use bubble sorting or fast sorting, the core of the sort is to compare the size of the two elements. If it is a number, we can compare it directly, but what if it is a string or two dict? There is no point in directly comparing the size of mathematics, so the process of comparison must be abstracted by functions. It is generally stipulated that for two elements and, if considered, then returned, if considered, then returned, x
y
x < y
-1
x == y
0
If considered x > y
, then returned 1
, so that the sorting algorithm does not care about the specific comparison process, Instead, they are sorted directly by comparison results.
Python's built-in sorted()
functions can sort the list:
>>> sorted([36, 5, 12, 9, 21])[5, 9, 12, 21, 36]
In addition, the sorted()
function is a higher-order function, and it can also receive a comparison function to implement a custom sort. For example, if you want to sort in reverse order, we can customize a reversed_cmp
function:
def reversed_cmp(x, y): if x > y: return -1 if x < y: return 1 return 0
By passing in a custom comparison function reversed_cmp
, you can sort in reverse order:
>>> sorted([36, 5, 12, 9, 21], reversed_cmp)[36, 21, 12, 9, 5]
Let's look at another example of string ordering:
>>> sorted([‘about‘, ‘bob‘, ‘Zoo‘, ‘Credit‘])[‘Credit‘, ‘Zoo‘, ‘about‘, ‘bob‘]
By default, the string is sorted by the size of ASCII, because, as a ‘Z‘ < ‘a‘
result, uppercase letters are Z
a
preceded by lowercase letters.
Now, we propose that the sort should be ignored in case of alphabetical order. To implement this algorithm, you do not have to change the existing code, as long as we can define the ignoring case of the comparison algorithm can be:
def cmp_ignore_case(s1, s2): u1 = s1.upper() u2 = s2.upper() if u1 < u2: return -1 if u1 > u2: return 1 return 0
Ignoring the case to compare two strings is actually the first to capitalize the strings (or all lowercase) before comparing them.
In this way, we sorted
can implement the sort of ignoring case by passing the above comparison function:
>>> sorted([‘about‘, ‘bob‘, ‘Zoo‘, ‘Credit‘], cmp_ignore_case)[‘about‘, ‘bob‘, ‘Credit‘, ‘Zoo‘]
As you can see from the above example, the abstraction of higher-order functions is very powerful, and the core code can be kept very concise.
function as return value
Higher-order functions can also return a function as a result value, in addition to the ability to accept functions as parameters.
Let's implement the summation of a mutable parameter. In general, the function of summing is defined like this:
def calc_sum(*args): ax = 0 for n in args: ax = ax + n return ax
But what if you don't need to sum it right away, but in the later code, and then calculate it as needed? You can return the SUM function without returning the result of the summation!
def lazy_sum(*args): def sum(): ax = 0 for n in args: ax = ax + n return ax return sum
When we call lazy_sum()
, we return the SUM function instead of summing the result:
>>> f = lazy_sum(1, 3, 5, 7, 9)>>> f<function sum at 0x10452f668>
When the function is called f
, the result of the sum is really computed:
>>> f()25
In this example, we define the function in the function lazy_sum
sum
, and the intrinsic function sum
can refer to lazy_sum
the parameters and local variables of the external function, and when the lazy_sum
function is returned sum
, the relevant parameters and variables are stored in the returned function, which is called " The program structure of the closure (Closure) has great power.
Note again that when we call lazy_sum()
, each call returns a new function, even if the same parameter is passed in:
>>> f1 = lazy_sum(1, 3, 5, 7, 9)>>> f2 = lazy_sum(1, 3, 5, 7, 9)>>> f1==f2False
f1()
and f2()
the results of the call are not affected.
Summary
Passing a function as a parameter, or returning a function as a return value, is called a higher-order function, and functional programming refers to this highly abstract programming paradigm.
Assuming that Python does not provide map()
a function, write a my_map()
function implementation with map()
the same functionality as your own.
A python-provided sum()
function can accept a list and sum it, write a prod()
function that accepts a list and takes advantage of the quadrature reduce()
.
Python high-order functions