Python_day4

 

Python

PHP

functional programming

Allows to pass a function itself as a parameter to another function, and also allows to return a function

 

Higher order functions

abs(), the call to the function is not the function itself

As a function itself, the function name is actually a variable that can change the pointer, but this is generally not done . The function name can be assigned to the variable so that the variable also points to the function

variable = abs

Higher-order functions: functions that can take functions as arguments

x = -5

y = 6

f = abs

print(add(x,y,f))

$a = "abs";

function test($a){

           echo $a(-2);

}

test($a);

map()

Apply a function to an iterable object

map(function,Iterable)

def f(x):

    return x*x

l = [1, 2, 3, 4, 5, 6, 7, 8, 9]

res = map(f,l)

print(list(res))

 

reduce()

Starting from the first two, into the function, the return value of the execution and the third number continue to be passed in until the execution is complete

reduce(function,Iterable)

from functools import reduce

def p(x,y):

    return x*y

l = [1,2,3]

print(reduce(p,l))

the end result is a number

If the Iterable has only one number, return the value directly

 

















Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326674891&siteId=291194637