Persistent Vectors
1 Example Usage
2 Native API Reference
pvector?
pvector
3 Comprehensions
for/  pvector
for*/  pvector
6.2.901.900

Persistent Vectors

 (require alexis/pvector) package: alexis-pvector

This provides an implementation of persistent, immutable vectors. They are implemented as 32-way bitmapped tries with a tail to provide truly constant time appends to the end of the vector. Data is shared between a vector and its subsequent functional modifications.

The persistent vectors provided by this module implement the gen:equal+hash generic interface, so they may be used as keys, and equal? will perform deep comparisons. Additionally, they implement three interfaces from alexis/collection, gen:countable, gen:collection, and gen:sequence, which contain the interface for interacting with them.

1 Example Usage

Persistent vectors may be created using the pvector constructor.

> (pvector 1 2 3 4)

#pv[1 2 3 4]

Afterwards, they can be interacted with via the functions from alexis/collection.

> (conj (pvector 1 2 3 4) 5)

#pv[1 2 3 4 5]

It is often useful to create an empty vector using (pvector), then extending it with some other sequence.

> (extend (pvector) (take 10 (in-naturals)))

#pv[0 1 2 3 4 5 6 7 8 9]

2 Native API Reference

The interface provided by alexis/pvector is small. Most of the functions for manipulating persistent vectors reside in alexis/collection. However, some functions are specific to persistent vectors.

procedure

(pvector? v)  boolean?

  v : any/c
Returns #t if v is a persistent vector, otherwise returns #f.

procedure

(pvector v ...)  pvector?

  v : any/c
Creates a new persistent vector with the v arguments as its contents. Calling this function with multiple arguments simply performs extend on the empty vector, (pvector), so it is no more efficient than using extend directly.

3 Comprehensions

syntax

(for/pvector (for-clause ...) body-or-break ... body)

syntax

(for*/pvector (for-clause ...) body-or-break ... body)

Equivalent to for/sequence or for*/sequence combined with (extend (pvector) ...) to collect the results into a persistent vector.