Immutable Persistent Data Structures

Simple Interactive JS Console

Immutable List

List are singly linked lists which support efficient O(1) addition and removal from the front using unshift(value) and shift(). Wikipedia Persistent Data Structure Linked Lists

Example Code

var a = ImmutableList.of(0, 1, 2, 3);
var b = a.push(4, 5, 6);
a.toArray(); // -> [0, 1, 2, 3]
b.toArray(); // -> [0, 1, 2, 3, 4, 5, 6]
      

Immutable Vector

Vectors are ordered indexed collections. Understanding Clojure's Persistent Vector

Example Code

var a = ImmutableVector.of(0, 1, 2, 3);
var b = a.push(4, 5, 6);
a.toArray(); // -> [0, 1, 2, 3]
b.toArray(); // -> [0, 1, 2, 3, 4, 5, 6]
      

Immutable HashMap

HashMap is an unordered collection of (key, value) pairs. Understanding Clojure's Persistent HashMap

Example Code

var a = ImmutableHashMap.of({a: 0, b: 1});
var b = a.set("c", 2);
a.toObject(); // -> {a: 0, b: 1}
b.toObject(); // -> {a: 0, b: 1, c: 2}
      

Immutable Set

Sets are a collection of unique values. It is a wrapper class where Set<V> = HashMap<V, Void>.

Example Code

var a = ImmutableSet.of(0, 1, 2, 3);
var b = a.set(2, 3, 4);
a.toArray(); // -> [0, 1, 2, 3]
b.toArray(); // -> [0, 1, 2, 3, 4]
      

Immutable Record

A Record is a wrapper class around a HashMap with rescrited fields.

Example Code

var Person = ImmutableRecord({
    name: null
}, "Person");
var a = new Person({name: "Bob"});
var b = a.set("name", "Billy");
a.toObject(); // -> {name: "Bob"}
b.toObject(); // -> {name: "Billy"}