Chapter 5. Composite data types
It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures.
Alan Perlis
- Persistence, sequences, and complexity
- Vectors: creating and using them in all their varieties
- Lists: Clojure’s code form data structure
- How to use persistent queues
- Persistent sets
- Thinking in maps
- Putting it all together: finding the position of items in a sequence
Clojure provides a rich set of composite data types and we’ll cover them all: vectors, lists, queues, sets, and maps. In this chapter, we’ll dig into the strengths and weaknesses of each. We’ll spend more time on vectors and maps than on the other types, because those two are used in a wider variety of circumstances and warrant the extra discussion. Finally, we’ll discuss the design of a simple function to leverage many of the lessons learned in this chapter, and you’ll gain specific insight into the preceding quote. By the way, we use the terms composite types and collections interchangeably, so please bear that in mind as we proceed.
Before we look at the primary collection types individually, we’ll discuss the things they have in common. For example, you may have heard of Clojure’s sequence abstraction—all the persistent collections use it, so we’ll examine that as well as some algorithmic complexity concepts we’ll be referring to throughout the chapter.