Higher-order function refers to function 1 that takes a function as an argument or returns the function as a result value.

1 sorted()

A more common higher-order function is sorted(), whose internal keyword argument key can take a function as an argument to specify collation 2 of values.

For example, if you give a len function to the keyword argument key, you can sort the list by word length.

animals = ['ox', 'giraffe', 'mouse', 'tiger', 'lion', 'deer', 'goose']
r = sorted(animals, key=len)
logging.info('r -> %s', r)

Copy the code

Running results:

INFO - r -> ['ox', 'lion', 'deer', 'mouse', 'tiger', 'goose', 'giraffe']
Copy the code

As you can see from the output, the longer the word, the lower the rank.

You can create a rhyming dictionary by spelling the words backwards and then ordering them. For example, when mouse and goose have the same ending sound, they are called rhymes. A rhyming dictionary is a dictionary that puts these words together.

def reverse(word):
    return word[::-1]


logging.info('r -> %s', r)
r = sorted(animals, key=reverse)
Copy the code

Running results:

INFO - r -> ['giraffe', 'goose', 'mouse', 'lion', 'deer', 'tiger', 'ox']
Copy the code

Start by defining a word reverse() function that uses word[::-1] to reverse order. Word [::-1] = word[-1:-len(word)-1:-1] = word[-1:-len(word)-1:-1]

2 Weakened map and filter functions

List inference has been weakened because it can replace what map and filter functions do.

Map () syntax: map(function, iterable…) Map calls function for each element in the argument sequence and treats the return value of each function function as element 4 of the new list.

The following example illustrates the basic usage of the map and filter functions, both of which have the corresponding form of list derivation substitution.

def multi3(x):
    return 3 * x


r = list(map(multi3, range(6)))
logging.info('r -> %s', r)

r = [multi3(n) for n in range(6)]
logging.info('r -> %s', r)

r = list(map(multi3, filter(lambda n: n % 2, range(6))))
logging.info('r -> %s', r)

r = [multi3(n) for n in range(6) if n % 2]
logging.info('r -> %s', r)
Copy the code

Running results:

INFO - r -> [0, 3, 6, 9, 12, 15]
INFO - r -> [0, 3, 6, 9, 12, 15]
INFO - r -> [3, 9, 15]
INFO - r -> [3, 9, 15]
Copy the code

The example begins by defining a multi3 function that multiplies the input parameter by 3 and returns it. The filter function defines a lambda expression to filter out odd numbers. The expression n % 2 evaluates the remainder, and returns 1 if it is odd; Returns 0 if it is even. Because if 1 returns true and if 0 returns false, there is no need for an explicit return. You can see that the list-derived code is much cleaner than the filter function.

3 Weakened reduce() function

Reduce () function syntax: reduce(function, iterable[, initializer])5. The function function takes two arguments. The reduce() function functions the first and second elements of the set, then functions the result with the third element, and finally yields a result. This function is most commonly used for summing, and there is now a better built-in sum() function that does the same thing, and is much more readable1.

from functools import reduce
from operator import add

r = reduce(add, range(100))
logging.info('r -> %s', r)
r = sum(range(100))
logging.info('r -> %s', r)
Copy the code

Running results:

INFO - r -> 4950
INFO - r -> 4950
Copy the code

The sum() function is much cleaner than the sum() function.


Use built-in functions for everything you can do with Python’s built-in functions.


[1] Luciano Ramalho (author), An Dao, Wu Ke (translator). Smooth Python[M]. People’s Posts and Telecommunications Press, Sorted () : sorted() : sorted() : sorted() : sorted() : sorted( To reduce () function.