- Hands-On Reactive Programming with Python
- Romain Picard
- 219字
- 2021-06-24 18:25:19
Higher-order functions
The last concept that is important to know when starting with functional programming is higher-order functions. A higher-order function is a function that accepts a function as an input parameter and/or returns a function as an output parameter. There are different reasons to use higher-order functions:
- By taking functions as input, higher-order functions allow you to implement generic behavior on different types of actions. This allows you to factorize some code.
- Returning a function as an output allows you to capture the values of input arguments, thanks to closures, and return a function that operates on a fixed set of parameters.
Both of these situations are covered in the example of the Closures section:
def act_on(what):
def exec(f):
f(what)
return exec
The exec function is a higher-order function that is an example of the first case. It takes a function as an input and calls it. The example is really trivial, but should any code be added in the exec function, it would add some behavior available to any function being passed as an input.
The second case is exactly what act_on does. The exec function that is returned by act_on embeds the value of what that was provided as an input. This is a very common way to save some context information and use it later.
- Cybersecurity:Attack and Defense Strategies
- Linux實戰
- 從零開始寫Linux內核:一書學透核心原理與實現
- BPEL and Java Cookbook
- Extending Puppet
- VMware Horizon View 6 Desktop Virtualization Cookbook
- 混沌工程實戰:手把手教你實現系統穩定性
- 直播系統開發:基于Nginx與Nginx-rtmp-module
- iOS 8開發指南
- Ubuntu Linux操作系統實用教程
- 統信UOS應用開發進階教程
- 大學計算機應用基礎實踐教程(Windows 7+MS Office 2010)
- 鴻蒙入門:HarmonyOS應用開發
- Implementing Domain-Specific Languages with Xtext and Xtend(Second Edition)
- Learning Joomla! 3 Extension Development(Third Edition)