11 Maps

 

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, 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]:

11.1 Creating

11.1.1 hash-map

11.1.2 array-map

11.1.3 sorted-map and sorted-map-by

11.1.4 create-struct, defstruct, struct-map, struct and accessor

11.1.5 zipmap

11.2 Accessing

11.2.1 keys and vals

11.2.2 find, key and val

11.2.3 select-keys and get-in

11.3 Processing

11.3.1 assoc, assoc-in and dissoc

11.3.2 update and update-in

11.3.3 merge and merge-with