Chapter 3. Collections and Data Structures
Maps, vectors, sets, and lists are the basic data structures provided by Clojure. As you’ve seen already, each of these has its own convenient literal notation:
'(a b :name 12.5) ;; list
['a 'b :name 12.5] ;; vector
{:name "Chas" :age 31} ;; map
#{1 2 3} ;; set
{Math/PI "~3.14"
[:composite "key"] 42
nil "nothing"} ;; another map
#{{:first-name "chas" :last-name "emerick"}
{:first-name "brian" :last-name "carper"}
{:first-name "christophe" :last-name "grand"}} ;; a set of mapsThese categories of data structures and their notations are likely familiar to you for the most part; particularly with regard to notation, Ruby and Python are quite similar. However, Clojure data structures have a couple of distinctive characteristics:
They are first and foremost used in terms of abstractions, not the details of concrete implementations.
They are immutable and persistent, both essential to Clojure’s flavor of efficient functional programming.
Each data structure has its own characteristics and idiomatic patterns of usage that we’ll explore progressively, but it is far more important to internalize the above points and what they imply about Clojure, its data structures, and how you can and should design your Clojure applications.
Abstractions over Implementations
It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures.[72]
—Alan J. Perlis in the foreword to Structure and Interpretation of Computer Programs
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access