## Higher-order functions of Python functional programming

we'll look at a few high-level Python Map function: Reduce, Filter, Sorted, .

**Map Function**

#### Definition

map()The function receives two parameters, one is the function and the other is Iterable to map apply the incoming function to each element of the sequence in turn and use the result as a new Iterable return.

Example

>>> def f (x) :

... return x*x

...

>>> r = map(f, [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ])

>>> r

<map object at 0x0000020AAF4C1668 >

>>> list(r)

[ 1 , 4 , 9 , 16 , 25 , 36 , 49 , 64, 81 , 100 ]

>>>

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

[ '1' , '2' , ' 3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' ]

### Reduce Function

#### Definition

reduce()The function is map()similar to that, receiving two parameters, one is a function and one is Iterable. The difference is that reduce()a function is applied to a sequence [x1, x2, x3, ...], and this function must receive two parameters, reduce()and continue to accumulate the result and the next element of the sequence, as follows:

reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)

#### Example

>>> from functools import reduce

>>> def fn (x, y) :

... return x* 10 + y

...

>>> reduce(fn, [ 1 , 3 , 5 , 7 , 9 ])

13579

# With map(), we can write a function that converts str to int

>>> def char2nums (s) :

... digits = { '0' : 0 , '1' : 1 , '2' : 2 , '3' : 3 , '4' : 4 , '5' : 5 , '6' : 6 , '7' : 7 , '8' : 8 , '9' : 9 }

... return digits[s]

...

>>> reduce(fn, map(char 2 nums, '1234' ))

1234

Using lambda expressions can further simplify:

> from functools import reduce

>>

DIGITS = { '0' : 0 , '1' : 1 , '2' : 2 , '3' : 3 , '4' : 4 , '5' : 5 , '6' : . 6 , '. 7' : . 7 , '. 8' : . 8 , '. 9' : . 9 }

>

> DEF char2num (S) :

> return the DIGITS [S]

>

> DEF str2int (S) :

> return the reduce ( the lambda X, Y: X * 10 + Y, Map (char2num, S))

>

#### Exercise

Use map and reduce write a str 2 float function to '123.456'convert a string to a floating point number 123.456:

def f (dig) :

dict = { '0' : 0 , '1' : 1 , '2' : 2 , '3' : 3 , '4' : 4 , '5' : 5 , '6' : 6 , '7' : 7 , '8' : 8 , '9' : 9 } return dict[dig]

def g (x, y) : return x* 10 +y

def h (a, b) : return (a/ 10 +b)

def str2float (s) : return reduce(g, map(f,s[:s.find( '.' )])) + reduce(h, map(f, s[ -1 :-(len(s)- s.find( '.' )): -1 ]))/ 10

### Filter Function

#### Definition

And map()the like, filter()also receives a function and a sequence. And map()the difference is, filter()the function passed successively to each element, based on the returned value True is False decided to keep or discard the element. filter()The function returns one Iterator, which is a lazy sequence.

Example

In a list, delete even numbers and keep only odd numbers:

def is_odd (n) : return n% 2

list(filter(is_odd, [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]))

Delete the empty string in a sequence:

def not_empty (s) : return s and s.strip()

list(filter(not_empty, [ 'A' , '' , 'B' , None , 'C' , '' ]))

### Exercise

The number refers back to reading from left to right and right to left reading is the same number, for example 12321, 909. Please use the filter()filter to find the number of times:

def is_palindrome (n) : return str(n)==str(n)[:: -1 ]

output = filter(is_palindrome, range( 1 , 1000 ))

### Sorted Function

#### Definition

sorted()In addition list to sorting functions , it can also receive a key function to implement custom sorting.

Example

>>> sorted([ 36 , 5 , -12 , 9 , -21 ])

[ -21 , -12 , 5 , 9 , 36 ]

>>> sorted([ 36 , 5 , -12 , 9 , -21 ], key=abs)

[ 5 , 9 , -12 , -21 , 36 ]