day16 Functional Programming

# Higher order functions 
# 1 received parameter is a function name of the function return value # 2 contains functions

# passed as a parameter to a function of another function
# DEF foo (n-): # = n-bar
# Print (n-)
#
# DEF bar (name):
# Print ( 'My name IS% S'% name)
#
# # foo (bar)
# # foo (bar ())
# foo (bar ( 'Alex'))

# returns the value contained in the function
DEF bar ():
Print ( 'from bar')
DEF foo ():
Print ( 'from foo')
return bar
n-= foo ()
n-()
DEF Hanle ():
Print ( 'from handle')
return Hanle
H = Hanle ()
H ()



DEF test1 ():
Print ( 'from test1')
DEF test2 ():
print('from handle')
return test1()
map函数
map(function, iterable, ...)

map ()  will do the mapping function in accordance with the specified sequence provided.

The first parameter argument function to call the function function of each element in the sequence, each time the function returns a new list of function return values.

>>> DEF Square (X):             # calculate the number of squares 
...      return X ** 2 
...
 >>> Map (Square, [1,2,3,4,5])    # calculates the square of each element of the list 
[1, 4, 9, 16, 25 ]
 >>> Map ( lambda X: X ** 2, [1, 2,. 3, 4,. 5])   # using lambda anonymous function 
[1, 4, 9, 16, 25 ] 
 
# two lists, a data list by adding the same position 
>>> Map ( the lambda X, Y: X + Y, [. 1,. 3,. 5,. 7,. 9], [2,. 4,. 6 , 8, 10 ]) 
[ 3, 7, 11, 15, 19]

filter function

Performing filtering operations on the specified sequence.

Its definition:

filter (function function, sequence sequence)

a parameter receiving function, returns a Boolean value. Sequence may be a list, tuple string.

filter () to the sequence parameter sequence each element of the function argument function call, call result is True element returned as the result of the last filter () function.
----------------
Disclaimer: This article is CSDN blogger "The Hills sea of love." Original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/qq_39969226/article/details/100173577

movie_people=['aaa_sb','bbb_sb','ccc','ddd_sb']
# def sb_show(n):
#     return n.endswith('sb')
def filter_test(func,array):
    ret=[]
    for p in array:
        if not func(p):
            ret.append(p)
    return ret
print(list(filter(lambda n:n.endswith('sb'),movie_people)))
reduce function
reduce(function, iterable[, initializer])

reduce ()  function parameter sequence elements have accumulated.                             

All data is a function of data set (a list, tuple, etc.) are the following: (two parameters) of the first set of first and second elements with the function operates in the function passed to reduce, obtained results further data to the third function operation function, to obtain a final result.

>>> DEF the Add (X, Y):             # adding two numbers 
...      return X + Y 
...
 >>> the reduce (the Add, [1,2,3,4,5])    # calculates list: 2 + + +. 3. 1. 4. 5 + 
15 
>>> the reduce ( lambda X, Y: X + Y, [1,2,3,4,5])   # using lambda anonymous function 
15

 

Guess you like

Origin www.cnblogs.com/douyunpeng/p/12626735.html