学习资料:
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.
例二:
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)
2019年5月22日 02:08
very good example
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.
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.
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.