1 Python higher-order functions
Parameter receiving function, or a function as a function of the result returned by the higher-order functions.
1.1 custom sort function
Requirements: Built sorted modeled function, achieve a sort function itself. Built-in function sorted function returns a new list, you can set ascending or descending, you can also set up a sort of function, but also a custom sort function to achieve this functionality.
The sort function to realize the idea: create a new list, traversing the value of the original list, and a new list of comparative decide how to insert into the new list.
a sort function version of realization, as follows:
. 1 DEF Sort (Iterable): 2 ret = [] . 3 for X in Iterable: . 4 for I, Y in the enumerate (ret): . 5 IF X> Y: # find the data on the ratio of ret large position inserted into the respective position, in descending order . 6 ret.insert (I, x) . 7 BREAK . 8 the else : # is not greater than, x is the smallest in the description of ret, it is added directly to the rear ret . 9 ret.append (x) 10 return ret . 11 12 is IF the __name__ == ' __main__ ' : 13 lst = [1,2,3,4,5,6,7,8] 14 print(sort(lst))
The code can only be achieved, in ascending order of arrangement, then the arrangement now want to control the way through an argument, how to achieve it? Look at the following code:
. 1 DEF Sort (Iterable, Reverse = False): 2 RET = [] . 3 for X in Iterable: . 4 for I, Y in the enumerate (RET): . 5 In Flag = X> Y IF Reverse the else X <Y # Default ordering is ascending . 6 IF in Flag: # found larger than the number of positions ret inserted into the respective position, in descending order . 7 ret.insert (I, x) . 8 BREAK . 9 the else : # is not greater than the minimum description ret x is in direct after it is added to the RET 10 ret.append (X) 11 return court
By boole judgment parameter value reverse, descending or ascending order to see used, the can will have x, y determines the size of the abstract function, as a function of a sorting pass sort function? Look at the following code:
1 def comparator(x, y): 2 if x > y: 3 return False 4 else: 5 return True 6 7 def sort(iterable, reverse=False, key=None): 8 ret = [] 9 for x in iterable: 10 for i, y in enumerate(ret): 11 flag = key(x, y) if reverse else not key(x, y) 12 if flag: #Is larger than the number found in a ret position inserted into the respective position, in descending order by default 13 is ret.insert (I, x) 14 BREAK 15 the else : # is not greater than, the smallest x is described, which is added directly to the ret ret in after 16 ret.append (X) . 17 return RET
The code in the sort function call, each had to pass a sorting function can be achieved, it can not be passed by the expression of a function as a default value parameters? This use to anonymous functions, as follows:
. 1 DEF Sort (Iterable, Reverse = False, Key = the lambda A, B: A> B): 2 RET = [] . 3 for X in Iterable: . 4 for I, Y in the enumerate (RET): . 5 In Flag = Key (X , Y) IF Not Reverse the else Not Key (X, Y) . 6 IF in Flag: # found larger than the number of positions ret inserted into the respective position, in descending order by default . 7 ret.insert (I, X) . 8 BREAK . 9 the else : # after no greater than, described in ret x is the smallest, which was added directly to ret 10 ret.append (x) 11 Return right
Step by step to achieve this code, the sort function has been implemented from a simple initial sorting, into ascending function may be provided and the final sorting, sorting and setting rules. This will sort function into a higher-order function.
1.2 built-in functions - higher-order functions
1.2.1 sorted function
sorted (iterable [, key] [, reverse]), the optional built-in function sorted function key parameter for providing a function, which is applied to the respective elements sorted. For example, if you want to be sorted according to word length, simply pass the key parameter len function, the following example:
Any single parameter can function as the value of key parameters, such as another realization: After reversing the letters of each word, in its sort. To achieve the following:
In functional programming, Python 3 also commonly used in higher-order functions, map, filter.
1.2.2 filter function
filter (function, iterable), the filter element may be an iterative object, it returns an iterator. function is a function of a single parameter, return bool value.
. 1 List (filter ( the lambda X: X% 3 == 0, [1,9,55,150, -3,78,28,123])) # filtered in the digital data is divisible by 3 columns
1.2.3 map function
map (function, * iterables), a plurality of elements iterables maps as specified function returns an iterator.
list(map(lambda x:2*x+1, range(5))) dict(map(lambda x: (x%5,x) , range(500)))
In the Python 3, map and return filter generator (an iterative device), so now is the direct replacement of their derivation type and generate a list of expressions.
2 currying Currying
Currying refers to the original parameters received two functions into a new procedure function receives a parameter. A new function returns to the original second argument is the argument of the function. Which corresponds to the form z = f (x, y) is converted to z = f (x) (y) form. For example:
1 # The adder function currying 2 DEF the Add (X, Y): . 3 return X + Y . 4 . 5 # currying . 6 DEF the Add (X): . 7 DEF inc is (Y): . 8 return X + Y . 9 return inc is 10 . 11 foo = the Add (2 ) 12 is Print (foo (. 3 )) 13 is # corresponds to 14 Print (the Add (2) (. 3))
Curried by known processes, the use of nested function currying when function can be converted into a currying function.
From the front of the closures, and now currying higher-order functions and function, these are the basic Python decorators, with these basic, you can learn in Python decorators.