寫 Python 代碼不可不知的函數(shù)式編程技術(shù)(留言送書)

來源 : 機(jī)器之心
本文對(duì) Python 中的函數(shù)式編程技術(shù)進(jìn)行了簡(jiǎn)單的入門介紹。

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ù)值?!?/span>
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



留言送書


推薦理由:
(1)零基礎(chǔ)也能快速入門。無論讀者是否從事計(jì)算機(jī)相關(guān)行業(yè),是否接觸過人工智能,都能通過本書實(shí)現(xiàn)快速入門。(2)全新視角介紹數(shù)學(xué)知識(shí)。采用計(jì)算機(jī)程序模擬數(shù)學(xué)推論的介紹方法,使數(shù)學(xué)知識(shí)更為清晰易懂,更容易讓初學(xué)者深入理解數(shù)學(xué)定理、公式的意義。(3)每章最后提供根據(jù)所在章的理論知識(shí)點(diǎn)精心設(shè)計(jì)的“綜合性實(shí)例”,讀者可以通過綜合案例進(jìn)行實(shí)踐操作,為以后的算法學(xué)習(xí)奠定基礎(chǔ)。(4)大量范例源碼+習(xí)題答案。本書所有示例都有清晰完整的源碼,每章之后設(shè)有習(xí)題并配套題目答案,講解清晰,解決讀者在學(xué)習(xí)中的所有困惑。?
推薦理由:
本書分為3部分:
1、基礎(chǔ)篇,帶領(lǐng)初學(xué)者實(shí)踐Python開發(fā)環(huán)境和掌握基本語法,同時(shí)對(duì)網(wǎng)絡(luò)協(xié)議、Web客戶端技術(shù)、數(shù)據(jù)庫(kù)建模編程等網(wǎng)絡(luò)編程基礎(chǔ)深入淺出地進(jìn)行學(xué)習(xí);
2、框架篇,學(xué)習(xí)當(dāng)前*流行的Python Web框架,即Django、Tornado、Flask和Twisted,達(dá)到對(duì)各種Python網(wǎng)絡(luò)技術(shù)融會(huì)貫通的目的;
3、實(shí)戰(zhàn)篇,分別對(duì)幾種常用WEB框架進(jìn)行項(xiàng)目實(shí)踐,利用其各自的特點(diǎn)開發(fā)適用于不同場(chǎng)景的網(wǎng)絡(luò)程序。
推薦理由:
內(nèi)容全面:借助5大Python工具庫(kù),實(shí)現(xiàn)數(shù)據(jù)分析從獲取到建模全流程覆蓋;貼合實(shí)際:不空講Python語法,清晰簡(jiǎn)明地介紹如何用Python來處理、分析數(shù)據(jù);熱點(diǎn)案例:覆蓋6大熱點(diǎn)應(yīng)用領(lǐng)域,可直接參考研發(fā),實(shí)現(xiàn)數(shù)據(jù)變現(xiàn);學(xué)習(xí)資源:提供所有案例源代碼和數(shù)據(jù),供讀者操作練習(xí),快速上手
活動(dòng)規(guī)則:
活動(dòng)截止時(shí)我們將從本文的精選留言中選出?六位粉絲的走心留言贈(zèng)送書籍,以上書籍任選一本免費(fèi)包郵贈(zèng)送~
活動(dòng)截止時(shí)間:?2021 年 10 月 16 日 16:00 整
1、Python 3.10 正式發(fā)布了!我發(fā)現(xiàn)了一個(gè)可怕的功能...
3、10 個(gè)“瘋狂”的 Python 項(xiàng)目創(chuàng)意
4、某公司領(lǐng)導(dǎo)稱“80后就該退出iT行業(yè)”,遭網(wǎng)友怒噴
5、稚暉君自制機(jī)械臂,能給葡萄縫針的那種,成本1萬塊,網(wǎng)友:能把腦子開源一下?

