作者:Raivat Shah
參與:魔王、Jamin
本文對 Python 中的函數式編程技術進行了簡單的入門介紹。近來,越來越多人使用函數式編程(functional programming)。因此,很多傳統的命令式語言(如 Java 和 Python)開始支持函數式編程技術。本文對 Python 中的函數式編程技術進行了簡單的入門介紹。def foo():
print("foo")
bar = foo
bar()
#will print "foo" to the console
class Greeter:
def __init__(self, greeting):
self.greeting = greeting
def __call__(self, name):
return self.greeting + " " + name
morning = Greeter("good morning") #creates the callable object
morning("john") # calling the object
#prints "good morning john" to the console
callable(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
def iterate(list_of_items):
for item in list_of_items:
print(item)
看起來很酷吧,但這只不過是一級抽象而已。如果我們想在對列表執行迭代時進行列印以外的其他操作要怎麼做呢?
這就是高階函數存在的意義。我們可以創建函數 iterate_custom,待執行迭代的列表和要對每個項應用的函數都是 iterate_custom 函數的輸入: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.
嵌套函數
函數還可以在其他函數內部,這就是「內部函數」。內部函數在創建輔助函數時非常有用,輔助函數即作為子模塊來支持主函數的小型可重用函數。在問題需要特定函數定義(參數類型或順序)時,我們可以使用輔助函數。這種不遵循傳統做法的操作使得解決問題變得更加簡單,示例參見:http://www-inst.eecs.berkeley.edu/~cs61a/sp12/lectures/lect4-2x3.pdf。假設你想定義一個斐波那契函數 fib(n),該函數只有一個參數 n,我們必須返回第 n 個斐波那契數。定義此類函數的一種可行方式是:使用輔助函數來追蹤斐波那契數列的前兩個項(因為斐波那契數是前兩個數之和)。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)
將該計算從函數主體移到函數參數,這具備非常強大的力量。因為它減少了遞歸方法中可能出現的冗餘計算。
如果我們想在未給函數命名之前寫一個函數要怎麼做?如果我們想寫一個簡短的單行函數(如上述示例中的函數 foo 或 mult)要怎麼做?我們可以在 Python 中使用 lambda 關鍵字來定義此類函數。示例如下:mult = lambda x, y: x * y
mult(1, 2) #returns 2
該 mult 函數的行為與使用傳統 def 關鍵字定義函數的行為相同。
注意:lambda 函數必須為單行,且不能包含程式設計師寫的返回語句。事實上,它們通常具備隱式的返回語句(在上面的示例中,函數想表達 return x * y,不過我們省略了 lambda 函數中的顯式返回語句)。lambda 函數更加強大和精準,因為我們還可以構建匿名函數(即沒有名稱的函數):(lambda x, y: x * y)(9, 10) #returns 90
當我們只需要一次性使用某函數時,這種方法非常方便。例如,當我們想填充字典時:import collections
pre_fill = collections.defaultdict(lambda: (0, 0))
#all dictionary keys and values are set to 0
def 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 函數返回的 map 對象可被類型轉換為 list,以方便使用。現在,我們無需顯式地定義 multiply_by_four 函數,而是定義 lambda 表達式:
modified_scores = list(map(lambda x: 4 * x, scores))
當我們想對集合內的所有值執行某項操作時,map 函數很有用。就像名稱所顯示的那樣,filter 函數可以幫助篩除不想要的項。例如,我們想要去除 scores 中的奇數,那麼我們可以使用 filter: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 = 32
Best 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