Chapter 13. More on functional programming
This chapter covers
- A refresher on higher-order functions
- Partial application and currying of functions
- Lexical closures
- Traditional object-oriented programming in Clojure
So far, you’ve seen a lot of the Clojure programming language, and you’ve used it to write programs that can access data stores, communicate on message queues, consume and provide you services, crunch large amounts of data, and more. In this chapter, we’ll revisit a fundamental concept of Clojure—that of functional programming.
Instead of approaching this from, say, a mathematical (or plain theoretical) standpoint, we’ll write code to explore some of the main ideas. We’ll start by implementing a few common higher-order functions used in functional programs, to help you become comfortable with recursion, lazy sequences, and functional abstraction and reuse in general.
Next, we’ll visit the land of currying and partial application. This exposure will give you further insight into functional programming and what you can do with it. Although partial application (and certainly currying) isn’t particularly widespread in Clojure code, sometimes it’s the perfect fit for the job.
The final stop will be to explore closures. The last section puts everything together to write a little object system that illustrates the ideas of OOP vis-à-vis functional programming.