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

By Xah Lee. Date: . Last updated: .

This page is WORK IN PROGRESS.

List

to create a list, use list. clojure.core/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). clojure.core/quote

(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 returns a segment (aka slice) of vector

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

(subvec ["a" "b" "c" "d"] 1 2) ; ["b"]
clojure.core/subvec

vec convert list to vector. clojure.core/vec

;; get first element
(get [3 4 5] 0) ; ⇒ 3

clojure.core/get

conj → join several collection datatypes, returns a vector.

(conj [3 5] '(2 4) [7 1] "aa" :bb) ; [3 5 (2 4) [7 1] "aa" :bb]

clojure.core/conj

key/value pairs (map, hashtable)

to create a key/value pair list, use this syntax {key1 val1 key2 val2 key3 val3}

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 {…}:

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

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

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

clojure.core/assoc

(dissoc map key1 key2 …) returns a new map, with keys removed. clojure.core/dissoc

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”. clojure.core/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 clojure.core/merge

merge-with → merge maps, but takes a function that act on duplicate keys. clojure.core/merge-with

get get a value by key. clojure.core/get

clojure.core/contains?

clojure.core/map?

clojure.core/keys

clojure.core/vals

set (collection)

the syntax is #{…}

clojure.core/hash-set

clojure.core/sorted-set

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 effciently converted to sequence view by seq. clojure.core/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) → Returns the first item in the collection. If coll is nil, returns nil. clojure.core/first

(first [3 4 5])  ; 3
(first '(7 8 9)) ; 7
(first [])       ; nil
(first nil)      ; nil

(rest coll) → Returns a sequence of collection coll without its first element. Result may be a empty sequence. clojure.core/rest

(cons x seq) → returns a new sequence, with first item x and rest items of seq. clojure.core/cons

(nth coll index not-found) → Returns the value at the index. Returns 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. clojure.core/nth

list? test if it's list.

(seq coll) → Returns a seq on the collection. If the collection is empty, returns nil. (seq nil) returns nil. seq also works on Strings, native Java arrays (of reference types) and any objects that implement Iterable. clojure.core/seq

(conj (list 3 4) 5)

clojure.core/filter

clojure.core/reduce

(drop n coll) → Returns a lazy sequence, with first n items of collection coll removed. clojure.core/drop

(drop 2 ["a" "b" "c"]) ; "c"

Ask me question on patreon