chapter eleven

11 Maps

 

Maps, along with sequences and vectors, are possibly the most flexible and used Clojure data structures. They support Clojure application design in several ways:

  • Attaching "names" to data that semantically belong together. Each key in a map is a name for a value.
  • Supporting immutability and persistency in a performance effective way (map uses the same HAMT Hash Array Mapped Trie data structure used by vectors [170]).
  • 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 which are often described using a mix of constructors names and actual Java types. The following are concrete implementations inheriting from a common clojure.lang.IPersistentMap interface:

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

11.3.4  reduce-kv

11.4  Map utilities

11.4.1  clojure.walk/keywordize-keys and clojure.walk/stringify-keys

11.4.2  clojure.set/rename-keys

11.4.3  clojure.set/map-invert