Data Collections

This page summarizes some of Clojure's key concepts regarding data collections. It also provides links to more detailed information.

Clojure provides persistent versions of several "collections": aggregate data structures such as Lists, Maps, Records, Sets, and Vectors. Because these data structures are defined in terms of (tiny!) interfaces, it is trivial to create other structures that use the same functions.

In particular, there are large collections of Sequence functions which can be used on a large (and extensible) variety of values (eg, files, lists, hashes, strings, sets, vectors, ???).

ArrayMaps

Clojure's ArrayMaps behave much like Maps, but they are implemented as arrays (eg, key val key val...). Consequently, the lookup performance is only acceptable for small maps. ArrayMaps are sometimes used in the manipulation of code forms.

Byte Arrays

Clojure is able to create arrays of (8-bit) bytes, using the byte-array function.

Lists

Clojure's Lists behave much like the singly-linked lists found in many other languages (eg, Lisp):

  • Insertion or deletion (always at the start) is fast: ~O(1).

  • Indexing to a specified position can be slow: ~O(n).

  • Clojure's Lists track their length (available via count).

  • By default, the first node of a List is a function, macro, or special form.

Maps

Clojure's Maps behave much like the hash tables found in many other languages (eg, Python):

  • Addition and indexing are reasonably fast: ~O(log n).

  • Both sorted (sorted-map) and unsorted (hash-map) versions are available.

Records

Clojure's Records are part of its (newish) Datatype facility: "By using defrecord you get generically manipulable information, plus the added benefits of type-driven polymorphism, and the structural efficiencies of fields."

Sets

Clojure's Sets behave much like Maps, save that the value is the same as the key. Again:

  • Addition and indexing are reasonably fast: ~O(log n).

  • Both sorted (sorted-set) and unsorted (hash-set) versions are available.

StructMaps

Clojure's StructMaps behave much like Maps, but their implementation shares a base set of keys, provides optimized accessor functions, etc. Most uses of StructMaps are now better served by Records.

Vectors

Clojure's Vectors behave much like the array data type found in many other languages (eg, Python). However, they are not dynamic (in general) or even sparse. When properly used, their performance is comparable to mutable versions:

  • Insertion or deletion (always at the end) is fast: ~O(1).

  • Indexing to a specified position is fast: ~O(1).


This wiki page is maintained by Rich Morin, an independent consultant specializing in software design, development, and documentation. Please feel free to email comments, inquiries, suggestions, etc!

Topic revision: r4 - 03 Mar 2013, RichMorin
This site is powered by Foswiki Copyright © by the contributing authors. All material on this wiki is the property of the contributing authors.
Foswiki version v2.1.6, Release Foswiki-2.1.6, Plugin API version 2.4
Ideas, requests, problems regarding CFCL Wiki? Send us email