Clojure: Collections (List, Vector, Set, Map, etc), Sequence

By Xah Lee. Date: . Last updated: .

This page is WORK IN PROGRESS.

List

to create a list, use list.

;; creating a list
(list 3 4 7)

to create a list but not eval its elements, use quote, for example, (quote (3 4 5)). A shortcut syntax is '(3 4 5).

(def x 9)

(list 3 x 7) ; (3 9 7)

;; create a list, but don't eval its elements
(quote (3 x 7)) ; (3 x 7)

;; shortcut syntax for quote
'(3 x 7) ; (3 x 7)

list datatype is implemented as “linked list”. This means, it's very fast to add or remove first element of list, or access first element, but slow to get nth element when n is large.

vector (array)

vector is like list but array-like in performance.

(vector 3 4 5)
;; a vector
[3 4 5]

use get or nth to access list like things.

subvec return a segment (aka slice) of vector

(subvec ["a" "b" "c" "d"] 1) ; ["b" "c" "d"]

(subvec ["a" "b" "c" "d"] 1 2) ; ["b"]

vec convert list to vector.

;; get first element
(get [3 4 5] 0) ; ⇒ 3
conj
Join several collection datatypes, return a vector.
(conj [3 5] '(2 4) [7 1] "aa" :bb) ; [3 5 (2 4) [7 1] "aa" :bb]

key-value pairs (map, hashtable)

to create a key/value pair list, use this syntax

{key1 value1 key2 value2 etc }

The key or value can be any type.

typically, the key's type are keywords datatype. That is, of the form :mykey

The following are 3 implementation of maps, with corresponding functions to create them. They all have the same syntax {…}:

array-map
Default. Ordered.
sorted-map
Ordered.
hash-map
Non-ordered. For large number of pairs.

assoc return a new map, but with key/val pair added or changed.

(def xx {:a 7 :b 3})

;; return a new map with added pair
(assoc xx :c 4) ; {:c 4, :b 3, :a 7}

(dissoc map key1 key2 ) return a new map, with keys removed.

map datatype is itself a function. It takes 1 arg “key”, and return the corresponding value to that key.

;; map datatype is itself a function. it takes a arg of key, return that key's value (or nil if key not exist)
({:k 3 :h 4} :k) ; ⇒ 3
({:k 3 :h 4} :y) ; ⇒ nil

don't confuse it with the function “map”.

keyword is also a function. It takes a map and return corresponding value to the keyword. Or nil if not exist.

;; keyword is also a function. It takes a map and return corresponding value. Or nil if not exist.
(:xx {:xx 3 , :yy 4}) ; ⇒ 3
(:hh {:xx 3 , :yy 4}) ; ⇒ nil

symbol is also a function. It takes a map and return corresponding value to the keyword. Or nil if not exist.

;; symbol is also a function. It takes a map and return corresponding value. Or nil if not exist.
('xx {'xx 3 , 'yy 4}) ; ⇒ 3
('hh {'xx 3 , 'yy 4}) ; ⇒ nil

merge

merge-with
merge maps, but takes a function that act on duplicate keys.

get get a value by key.

set (collection)

the syntax is #{…}

Sequence Datatype

http://clojure.org/reference/data_structures

http://clojure.org/reference/sequences

http://clojure.org/lazy

Sequence is the general interface to collection datatype. Any of list, vector, map, …. There are many functions that works one sequence.

sequence is a datatype. Most collections can be efficiently converted to sequence view by seq.

most sequence functions call seq internally, as first thing.

data types that can be efficiently worked as sequence are called “sequenceable type”.

ISeq is a interface for all list like things.

list, vector, hashmap … are all implementation of ISeq

Clojure core data structures are immutable. Meaning, they return a new value.

sequenceable types includes {list, vector, set, map, strings} and more.

Sequence Functions

(first coll)
Return the first item in the collection. If coll is nil, return nil.
(first [3 4 5])  ; 3
(first '(7 8 9)) ; 7
(first [])       ; nil
(first nil)      ; nil
(rest coll)
Return a sequence of collection coll without its first element. Result may be a empty sequence.
(cons x seq)
Return a new sequence, with first item x and rest items of seq.
(nth coll index not-found)
Return the value at the index. Return nil if index is larger than the last element's index. nth throws an exception unless not-found is supplied. nth also works for strings, Java arrays, regex Matchers and Lists, and, in O(n) time, for sequences.

list? test if it's list.

(seq coll)
Return a seq on the collection. If the collection is empty, return nil. (seq nil) return nil. seq also works on Strings, native Java arrays (of reference types) and any objects that implement Iterable.
(conj (list 3 4) 5)
(drop n coll)
Return a lazy sequence, with first n items of collection coll removed.
(drop 2 ["a" "b" "c"]) ; "c"