Python High-order function "Go"

Source: Internet
Author: User
Tags abs function definition

function is a kind of encapsulation supported by Python, we can decompose complex tasks into simple tasks by splitting large pieces of code into functions through a layer of function calls, which can be called process-oriented programming. function is the basic unit of process-oriented program design.

and functional programming (note that more than one "type" word)--functional programming, although it can also be attributed to the process-oriented programming, but the idea is closer to the mathematical calculation.

We first have to understand the concepts of computer (computer) and computational (Compute).

At the level of the computer, the CPU executes the subtraction instruction code, as well as various conditional judgments and jump instructions, so, assembly language is the most close to the computer languages.

And the calculation of the exponential meaning, the more abstract calculation, the farther away from the computer hardware.

corresponding to the programming language, is the lower level of the language, the more close to the computer, low degree of abstraction, implementation of high efficiency, such as C language, the more advanced language, the more close to the computation, high degree of abstraction, inefficient execution, such as Lisp language.

Functional programming is a very high degree of abstraction of the programming paradigm, the purely functional programming language functions are not variable, so any function, as long as the input is determined, the output is OK, this pure function we call no side effects. In the case of programming languages that allow the use of variables, because of the variable state inside the function, the same input may get different output, so this function has side effects.

One of the features of functional programming is that it allows the function itself to be passed as a parameter to another function, and also allows a function to be returned!

Python provides partial support for functional programming. Because Python allows the use of variables, Python is not a purely functional programming language.

Higher-order function variables can point to functions

For example, in Python's built-in function for absolute values abs() , call the function with the following code:

In [1]: abs(3+4j)Out[1]: 5.0

But what if it was written only abs ?

In [2]: absOut[2]: <function abs>

Visible, abs(3+4j) is the function call, but abs the function itself.

To get the result of a function call, we can assign the result to the variable:

In [3]: x = abs(-10)In [4]: xOut[4]: 10

But what if the function itself is assigned to a variable?

In [5]: f = absIn [6]: fOut[6]: <function abs>

Conclusion: The function itself can also be assigned to the variable, that is: The variable can point to the function.

If a variable points to a function, can it be called by this variable? Verify with code:

In [7]: f = absIn [8]: f(-10)Out[8]: 10

Success! The description variable f is now pointing to the abs function itself.

The function name is also a variable

So what is the function name? The function name is actually a variable pointing to the function! For abs() This function, it is perfectly possible to think of a function name as abs a variable, which points to an absolute value!

If you abs point to other objects, what happens?

In [9]: abs = 10In [10]: abs(-10)---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)<ipython-input-10-c432e3f1fd6c> in <module>()----> 1 abs(-10)TypeError: 'int' object is not callable

absafter the pointer 10 is pointed, it cannot be abs(-10) called by the function! Because abs this variable does not point to the absolute value function!

Of course, the actual code must not be written like this, this is to illustrate the function name is also a variable. To restore abs The function, restart the Python interactive environment.

Note: Since the abs function is actually defined in __builtin__ the module, it is needed to make the pointer to the modified abs variable take effect in other modules as well __builtin__.abs = 10 .

Incoming function

Since the variable can point to a function, the function's arguments can receive the variable, then one function can receive another function as a parameter, which is called the higher order function.

One of the simplest high-order functions:

def add(x, y, f):    return f(x) + f(y)

When we call add(-5, 6, abs) , Parameters x , y and f respectively receive -5 , 6 and, according to the abs function definition, we can deduce the calculation process as:

x ==> -5y ==> 6f ==> absf(x) + f(y) ==> abs(-5) + abs(6) ==> 11

Verify with code:

>>> add(-5, 6, abs)11

Writing higher-order functions allows the parameters of a function to receive other functions.

Neckyan function Map/reduce

Python has built map() -in and reduce() functions.

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]

map()The first parameter passed in is the f function object itself.

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 not only calculate the simple f (x) =x2, but also can calculate any complex function, for example, the list of all the numbers into a string:

>>> map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9])['1', '2', '3', '4', '5', '6', '7', '8', '9']

Only one line of code is required.

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

In Python 3, the reduce () function has been removed from the global namespace and is now placed in the Fucntools module with the first
Into

>>> def fn(x, y):...     return x * 10 + y... >>> reduce(fn, [1, 3, 5, 7, 9])Traceback (most recent call last):  File "<input>", line 1, in <module>    reduce(fn, [1, 3, 5, 7, 9])NameError: name 'reduce' is not defined>>> from functools import reduce>>> 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 char2num(s):...     kw = {...         '0': 0,...         '1': 1,...         '2': 2,...         '3': 3,...         '4': 4,...         '5': 5,...         '6': 6,...         '7': 7,...         '8': 8,...         '9': 9...         }...     

str2intthe 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 later.

Practice

The use map() of functions, the user entered the non-standard English name, the first letter capitalized, other lowercase canonical name. Input: [‘adam‘, ‘LISA‘, ‘barT‘] , Output: [‘Adam‘, ‘Lisa‘, ‘Bart‘] .

>>> def capitalizeMore(nameList):...     return map(lambda x: x.capitalize(), nameList)...     ... >>> for i in capitalizeMore(['adam', 'LISA', 'barT']):...     print(i)...     

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() .

>>> def prod(x, y):...     return x * y... >>> reduce(prod, [2, 4, 5, 7, 12])3360
Filter

Python's built-in filter() functions are used to filter the sequence.

and map() similar, filter() also receive a function and a sequence. and map() different, the filter() incoming function is applied sequentially to each element, and then True False the element is persisted or discarded based on the return value.

For example, in a list, delete even numbers, keep only odd numbers, and you can write:

def is_odd(n):    return n % 2 == 1filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15])# 结果: [1, 5, 9, 15]

To delete an empty string from a sequence, you can write:

def not_empty(s):    return s and s.strip()filter(not_empty, ['A', '', 'B', None, 'C', '  '])# 结果: ['A', 'B', 'C']

filter()the key to using this higher-order function is to implement a "filter" function correctly.

Practice

Please try to filter() delete the prime number of 1~100.

>>> import math>>> def is_sqr(x):...     r = int(math.sqrt(x))...     return r*r == x... >>> filter(is_sqr, range(1, 101))<filter object at 0x7fb4802ec4a8>>>> list(filter(is_sqr, range(1, 101)))[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Sorted sorting algorithm

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(['bob', 'about', '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(['bob', 'about', '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.

Reference
    • MU Lesson Web Tutorial Notes
    • Liaoche Teacher's website

      ---restore content ends---

Python High-order function "Go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.