Higher-Order Functions in Python

BabbleDay posted @ 2015年6月29日 20:04 in 见过的PL们 with tags python high-order function , 731 阅读

学习资料:

http://composingprograms.com/pages/16-higher-order-functions.html

定义:

Higher-Order functions can accept other functions as arguments or return functions as values.

例一:计算黄金比例

 

def improve(update, close, guess=1):
    while not close(guess):
        guess = update(guess)
    return guess

def golden_update(guess):
    return 1/guess + 1

def square_close_to_successor(guess):
    return approx_eq(guess * guess,
                     guess + 1)

def approx_eq(x, y, tolerance=1e-3):
    return abs(x - y) < tolerance

phi = improve(golden_update,
              square_close_to_successor)

 

Two key advantages of lexical scoping in Python.

  • 函数命名只与定义环境有关,与执行环境无关
    The names of a local function do not interfere with names external to the function in which it is defined, because the local function name will be bound in the current local environment in which it was defined, rather than the global environment.

     
  • 执行函数由内向外延展
    A local function can access the environment of the enclosing function, because the body of the local function is evaluated in an environment that extends the evaluation environment in which it was defined. 

例二:

def average(x, y):
    return (x + y)/2

def improve(update, close, guess=1):
    while not close(guess):
        guess = update(guess)
    return guess

def approx_eq(x, y, tolerance=1e-3):
    return abs(x - y) < tolerance

def sqrt(a):
    def sqrt_update(x):
        return average(x, a/x)
    def sqrt_close(x):
        return approx_eq(x * x, a)
    return improve(sqrt_update, sqrt_close)

result = sqrt(256)

 

函数计算时层层隔离,名字重复无影响。但是对于大工程来说,明明冲突可能给自己带来麻烦,比如需要重命名的时候。

 

def square(x):
    return x * x

def successor(x):
    return x + 1

def compose1(f, g):
    def h(x):
        return f(g(x))
    return h

def f(x):
    """Never called."""
    return -x

square_successor = compose1(square, successor)
result = square_successor(12)

 

Avatar_small
cleaning company dub 说:
2020年2月22日 03:19

Another why people sometimes don't utilize a commercially aware cleaning service plan is that they will be not of course how often collectively have professional upholstery cleaning done where they don't get experience to go about selecting the best service for a needs. In most instances, they will only need to do high quality cleaning at the time every a few months to yearly, depending relating to the depreciation on all the carpet and you need to traffic all the carpets can get. However, if there is pets, little children and superior traffic, then housecleaning carpets every ninety days might be necessary.

Avatar_small
BSNL Customer Care N 说:
2023年2月05日 21:36

BSNL customer care number of each state regarding to BSNL services towards toll free and paid numbers which are accessed from own and other networks across the country. BSNL Customer Care No The new digital era-based support system contributing a lion’s sharing related to all telecom services, also find the new email address providing BSNL complaint process for payment failed issues for any digital payment service failure related issues like repayment.

Avatar_small
pavzi.com 说:
2024年1月11日 21:50

Pavzi website is a multiple Niche or category website which will ensure to provide information and resources on each and every topic. Some of the evergreen topics you will see on our website are Career, Job Recruitment, Educational, Technology, Reviews and others. pavzi.com We are targeting mostly so it is true that Tech, Finance, and Product Reviews. The only reason we have started this website is to make this site the need for your daily search use.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter