上QQ阅读APP看书,第一时间看更新
Functions and Closures
In the previous chapter, we had an overview of Functional Programming (FP) and the Swift programming language. It introduced some of the key concepts of functions. As functions are the fundamental building blocks in FP, this chapter pes deeper into the subject and explains all the aspects related to the definition and usage of functions in Swift and FP, together with coding examples.
This chapter starts with the Swift function and method syntax, continues with other related topics such as function types and tuples, and finally concludes with FP topics such as first-class functions, higher-order functions, function composition, closures, currying, recursion, and memoization.
This chapter will cover the following topics by coding examples:
- General syntax of functions
- Defining and using function parameters
- Setting internal and external parameters
- Setting default parameter values
- Defining and using variadic functions
- Returning values from functions
- Defining and using nested functions
- Function types
- Pure functions
- First class functions
- Higher-order functions
- Function composition
- Custom operator definition
- Defining and using closures
- Function currying
- Recursion
- Memoization