Maps, along with sequences and vectors, are possibly the most flexible and used Clojure data structure. They support Clojure application design in several ways:
- Attaching "labels" to data. Each key in a map is a name for a value. Named values are easier to reason about in code and supported by other features of the language (such as destructuring.
- Supporting immutability and persistency in an efficient way (map uses the same HAMT Hash Array Mapped Trie data structure used by vectors [174]).
- Allowing lookup by key, including using the map itself as a function.
- The standard library contains many functions dedicated to map manipulation (such as “assoc, assoc-in and dissoc”, merge, select-keys, etc.). The description of such functions is the topic of this chapter.
Clojure contains several kind of maps (or map-like objects) which are described using a mix of their constructors names and actual Java types (class names belong to the clojure.lang
package unless otherwise specified). The types in the following list implement IPersistentMap
, which is the most specific map interface [175]: