"ImmutableVector" (Data Structure)


represents an immutable vector where modifications generate a new data structure and the elements are general expressions.


  • An immutable vector is useful for efficiently adding and removing elements without modifying the data structure in place.
  • An immutable vector uses structural sharing, which results in performance-bound guarantees.
  • CreateDataStructure["ImmutableVector"]create a new empty "ImmutableVector"
    CreateDataStructure["ImmutableVector",elems]create a new "ImmutableVector" containing elems
    Typed[x,"ImmutableVector"]give x the type "ImmutableVector"
  • For a data structure of type "ImmutableVector", the following operations can be used:
  • ds["Append",x]return a new immutable vector with x appended to dstime: O(log n)
    ds["DropLast"]return a new immutable vector with the last element of ds droppedtime: O(log n)
    ds["Elements"]return a list of the elements of dstime: O(n)
    ds["EmptyQ"]True, if ds has no elementstime: O(1)
    ds["Fold",fun,init]apply fun to the elements of ds, starting with init, accumulating a resulttime: O(n)
    ds["JoinBack",elems]join elems to the back of dstime: O(nelems)
    ds["Length"]the number of elements stored in dstime: O(1)
    ds["Part",i]give the i^(th) part of dstime: O(log n)
    ds["ReplacePart",i,x]return a new immutable vector with the i^(th) part replaced by xtime: O(log n)
    ds["Visualization"]return a visualization of dstime: O(n)
  • The following functions are also supported:
  • dsi===dsjTrue, if dsi equals dsj
    FullForm[ds]full form of ds
    Information[ds]information about ds
    InputForm[ds]input form of ds
    Normal[ds]convert ds to a normal expression


open allclose all

Basic Examples  (2)

A new "ImmutableVector" can be created with CreateDataStructure:

Elements can be appended:

Return the length:

The length of the original vector did not change:

Extract the first element:

Replace an element:

ds1 remains unchanged:

ds2 has the replaced value:

Return expression version of ds:

It is efficient to append elements:

A visualization of the data structure can be generated:

Sum all elements:

Scope  (2)

Efficiency  (1)

"ImmutableVector" is efficient at appending elements:

Regular list expressions are not as fast for appending:

Association is also fast for appending, but gives a key rather than an index view into the data:

Information  (1)

A new "ImmutableVector" can be created with CreateDataStructure:

Information about the data structure ds: