This chapter covers:
- Defining Higher Order Functions that pass functions as parameters to other functions
- Writing loops in a functional way using recursion
- Abstracting Higher Order Functions to become Polymorphic
- Calling Higher Order Functions with Anonymous Functions
- Following types to implement Polymorphic Functions
In chapter 1, we committed ourselves to using only pure functions. From this commitment, a question naturally arises: how do we write even the simplest of programs? Most of us are used to thinking of programs as sequences of instructions that are executed in order, where each instruction has some kind of effect. In this chapter, we begin learning how to write programs in the Kotlin language just by combining pure functions.
In this chapter we’ll introduce some of the basic techniques for how to write functional programs. We’ll discuss how to write loops using tail recursive functions, and we’ll introduce higher-order functions (HOFs). HOFs are functions that take other functions as arguments and may themselves return functions as their output. We’ll also look at some examples of polymorphic HOFs where we use types to guide us toward an implementation.