Kết hợp chức năng là cách kết hợp hai hoặc nhiều chức năng theo cách sao cho đầu ra của một chức năng trở thành đầu vào của chức năng thứ hai, v.v. Ví dụ: giả sử có hai hàm “F” và “G” và thành phần của chúng có thể được biểu diễn dưới dạng F[G[x]] trong đó “x” là đối số và đầu ra của hàm G[x] sẽ trở thành đầu vào của F
Ví dụ
# Function to add 2
# to a number
def
add[x]:
return
x
+
Adding 2 to 5 and multiplying the result with 2: 140
Adding 2 to 5 and multiplying the result with 2: 141
Adding 2 to 5 and multiplying the result with 2: 142
Adding 2 to 5 and multiplying the result with 2: 143
def
Adding 2 to 5 and multiplying the result with 2: 145
return
x
Adding 2 to 5 and multiplying the result with 2: 149
Adding 2 to 5 and multiplying the result with 2: 140
Adding 2 to 5 and multiplying the result with 2: 141
# Function to add 2
2
# Function to add 2
3
# Function to add 2
4
# Function to add 2
5
# Function to add 2
6_______43_______7# Function to add 2
8# Function to add 2
9
# to a number
0# to a number
1# to a number
2# to a number
3
đầu ra
Adding 2 to 5 and multiplying the result with 2: 14
Giải trình
Đầu tiên hàm # to a number
4 được gọi trên đầu vào 5. # to a number
4 thêm 2 vào đầu vào và đầu ra là 7, được coi là đầu vào cho # to a number
6 nhân nó với 2 và đầu ra là 14
Cách tốt hơn để thực hiện sáng tác
Có một cách tốt hơn để thực hiện thành phần của Chức năng. Chúng ta có thể tạo một hàm đặc biệt có thể kết hợp hai hàm bất kỳ
# to a number
7
# to a number
8
# to a number
9
def
def
1
return
def
4 def
5
Adding 2 to 5 and multiplying the result with 2: 141
def
7
def
add[x]:
return
x
+
Adding 2 to 5 and multiplying the result with 2: 140
Adding 2 to 5 and multiplying the result with 2: 141
add[x]:
6
def
Adding 2 to 5 and multiplying the result with 2: 145
return
x
Adding 2 to 5 and multiplying the result with 2: 149
Adding 2 to 5 and multiplying the result with 2: 140
Adding 2 to 5 and multiplying the result with 2: 141
5
6
7
8
9 return
0
Adding 2 to 5 and multiplying the result with 2: 141
# Function to add 2
6_______43_______7# Function to add 2
8return
5
# to a number
0return
7# to a number
2return
9
đầu ra
Adding 2 to 5 and multiplying the result with 2: 14
Soạn N số chức năng
Chúng ta có thể soạn bất kỳ số lượng hàm nào bằng cách sửa đổi phương thức trên
x
0
x
1
# to a number
9
def
def
1
x
5
return
def
4 def
5
Adding 2 to 5 and multiplying the result with 2: 141
def
7
def
add[x]:
return
x
+
Adding 2 to 5 and multiplying the result with 2: 140
Adding 2 to 5 and multiplying the result with 2: 141
add[x]:
6
def
Adding 2 to 5 and multiplying the result with 2: 145
return
x
Adding 2 to 5 and multiplying the result with 2: 149
Adding 2 to 5 and multiplying the result with 2: 140
Adding 2 to 5 and multiplying the result with 2: 141
Adding 2 to 5 and multiplying the result with 2: 1409
def
Adding 2 to 5 and multiplying the result with 2: 1411
return
x
Adding 2 to 5 and multiplying the result with 2: 1415
Adding 2 to 5 and multiplying the result with 2: 1416
Adding 2 to 5 and multiplying the result with 2: 141
5
Adding 2 to 5 and multiplying the result with 2: 1419
Adding 2 to 5 and multiplying the result with 2: 1420
Adding 2 to 5 and multiplying the result with 2: 1421
Adding 2 to 5 and multiplying the result with 2: 1422_______47_______9
Adding 2 to 5 and multiplying the result with 2: 1424
Adding 2 to 5 and multiplying the result with 2: 1425
Adding 2 to 5 and multiplying the result with 2: 1426
Adding 2 to 5 and multiplying the result with 2: 1427______0_______28
Adding 2 to 5 and multiplying the result with 2: 141
Adding 2 to 5 and multiplying the result with 2: 141
# Function to add 2
6_______43_______7
Adding 2 to 5 and multiplying the result with 2: 1433
return
5# to a number
0
Adding 2 to 5 and multiplying the result with 2: 1436
# to a number
2return
9đầu ra
Cộng 2 với 5, sau đó trừ 1 và nhân kết quả với 2. 12
Bây giờ chúng ta sẽ sửa đổi composite_function của mình thành một hàm có thể soạn bất kỳ số lượng hàm nào thay vì hai hàm bằng cách sử dụng hàm reduce[] từ thư viện funcools