Python 函數(shù)式編程,看這一篇就夠了!

def?foo():
???print("foo")bar?=?foo
bar()
#will?print?"foo"?to?the?consoleclass?Greeter:
???def?__init__(self,?greeting):
??????self.greeting?=?greeting
???def?__call__(self,?name):
??????return?self.greeting?+?"?"?+?namemorning?=?Greeter("good?morning")?#creates?the?callable?object
morning("john")?#?calling?the?object
#prints?"good?morning?john"?to?the?consolecallable(morning)?#true
callable(145)?#false.?int?is?not?callable.?#?store?in?dictionary
mapping?=?{
???0?:?foo,
???1?:?bar
}
x?=?input()?#get?integer?value?from?user
mapping[x]()?#call?the?func?returned?by?dictionary?access「高階函數(shù)允許我們對(duì)動(dòng)作執(zhí)行抽象,而不只是抽象數(shù)值。」
def?iterate(list_of_items):
????for?item?in?list_of_items:
????????print(item)
看起來很酷吧,但這只不過是一級(jí)抽象而已。如果我們想在對(duì)列表執(zhí)行迭代時(shí)進(jìn)行打印以外的其他操作要怎么做呢?
def?iterate_custom(list_of_items,?custom_func):
???for?item?in?list_of_items:
????????custom_func(item)def?add(x,?y):
????return?x?+?y
def?sub(x,?y):
????return?x?-?y
def?mult(x,?y):
????return?x?*?y
def?calculator(opcode):
????if?opcode?==?1:
???????return?add
????elif?opcode?==?2:
???????return?sub
????else:
???????return?mult?
my_calc?=?calculator(2)?#my?calc?is?a?subtractor
my_calc(5,?4)?#returns?5?-?4?=?1?
my_calc?=?calculator(9)?#my?calc?is?now?a?multiplier
my_calc(5,?4)?#returns?5?x?4?=?20.?
嵌套函數(shù)
def?fib(n):
????def?fib_helper(fk1,?fk,?k):
????????if?n?==?k:
???????????return?fk
????????else:
???????????return?fib_helper(fk,?fk1+fk,?k+1)
????if?n?<=?1:
???????return?n
????else:
???????return?fib_helper(0,?1,?1)
將該計(jì)算從函數(shù)主體移到函數(shù)參數(shù),這具備非常強(qiáng)大的力量。因?yàn)樗鼫p少了遞歸方法中可能出現(xiàn)的冗余計(jì)算。
mult?=?lambda?x,?y:?x?*?y
mult(1,?2)?#returns?2
該 mult 函數(shù)的行為與使用傳統(tǒng) def 關(guān)鍵字定義函數(shù)的行為相同。
(lambda?x,?y:?x?*?y)(9,?10)?#returns?90import?collections
pre_fill?=?collections.defaultdict(lambda:?(0,?0))
#all?dictionary?keys?and?values?are?set?to?0def?multiply_by_four(x):
????return?x?*?4
scores?=?[3,?6,?8,?3,?5,?7]
modified_scores?=?list(map(multiply_by_four,?scores))
#modified?scores?is?now?[12,?24,?32,?12,?20,?28]
在 Python 3 中,map 函數(shù)返回的 map 對(duì)象可被類型轉(zhuǎn)換為 list,以方便使用。現(xiàn)在,我們無需顯式地定義 multiply_by_four 函數(shù),而是定義 lambda 表達(dá)式:
modified_scores?=?list(map(lambda?x:?4?*?x,?scores))even_scores?=?list(filter(lambda?x:?True?if?(x?%?2?==?0)?else?False,?scores))
#even_scores?=?[6,?8]sum_scores?=?reduce((lambda?x,?y:?x?+?y),?scores)
#sum_scores?=?32Best Practices for Using Functional Programming in Python:https://kite.com/blog/python/functional-programming/
Functional Programming Tutorials and Notes:https://www.hackerearth.com/zh/practice/python/functional-programming/functional-programming-1/tutorial/
原文鏈接:https://medium.com/better-programming/introduction-to-functional-programming-in-python-3d26cd9cbfd7
全新機(jī)器人公眾號(hào)上線啦,歡迎調(diào)戲!
推薦閱讀:
入門:?最全的零基礎(chǔ)學(xué)Python的問題? |?零基礎(chǔ)學(xué)了8個(gè)月的Python??|?實(shí)戰(zhàn)項(xiàng)目?|學(xué)Python就是這條捷徑
干貨:爬取豆瓣短評(píng),電影《后來的我們》?|?38年NBA最佳球員分析?|? ?從萬眾期待到口碑撲街!唐探3令人失望? |?笑看新倚天屠龍記?|?燈謎答題王?|用Python做個(gè)海量小姐姐素描圖?|碟中諜這么火,我用機(jī)器學(xué)習(xí)做個(gè)迷你推薦系統(tǒng)電影
趣味:彈球游戲? |?九宮格? |?漂亮的花?|?兩百行Python《天天酷跑》游戲!
AI:?會(huì)做詩的機(jī)器人?|?給圖片上色?|?預(yù)測(cè)收入?|?碟中諜這么火,我用機(jī)器學(xué)習(xí)做個(gè)迷你推薦系統(tǒng)電影
小工具:?Pdf轉(zhuǎn)Word,輕松搞定表格和水印!?|?一鍵把html網(wǎng)頁保存為pdf!|??再見PDF提取收費(fèi)!?|?用90行代碼打造最強(qiáng)PDF轉(zhuǎn)換器,word、PPT、excel、markdown、html一鍵轉(zhuǎn)換?|?制作一款釘釘?shù)蛢r(jià)機(jī)票提示器!?|60行代碼做了一個(gè)語音壁紙切換器天天看小姐姐!|
年度爆款文案
點(diǎn)閱讀原文,看B站我的視頻!

