fsprojects/FSharpx.Collections


PersistentVector

A Vector is a collection of values indexed by contiguous integers. Vectors support access to items by index in log32N hops. Count is O(1). Conj puts the item at the end of the vector.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
// Create an empty PersistentVector and add some elements
PersistentVector<string> vector =
    PersistentVector<string>.Empty()
        .Conj("hello")
        .Conj("world")
        .Conj("!");

Console.WriteLine(vector[0]); // hello
Console.WriteLine(vector[2]); // !            

// Check no. of elements in the PersistentVector
Console.WriteLine(vector.Length);  // 3

PersistentVectors are immutable and therefor allow to create new version without destruction of the old ones.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
PersistentVector<string> vector2 = vector.Conj("!!!").Update(0,"hi");

Console.WriteLine(vector2[0]); // hi
Console.WriteLine(vector[0]);  // hello
Console.WriteLine(vector2[3]); // !!!

Console.WriteLine(vector.Length);  // 3
Console.WriteLine(vector2.Length); // 4

// remove the last element from a PersistentVector
PersistentVector<string> vector3 = vector2.Initial;

Console.WriteLine(vector3.Length); // 3
Fork me on GitHub