Persistent Vectors
1 Example Usage
2 Native API Reference
pvector?
pvector
build-pvector
make-pvector
3 Comprehensions
for/  pvector
for*/  pvector
6.3.90.900

Persistent Vectors

 (require data/pvector) package: 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 data/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 data/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 data/pvector is small. Most of the functions for manipulating persistent vectors reside in data/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.

procedure

(build-pvector n proc)  pvector?

  n : exact-nonnegative-integer?
  proc : (exact-nonnegative-integer? . -> . any/c)
Creates a new persistent vector of length n by applying proc to each integer, i, from 0 to n-1 in order. The ith element of the vector will be the value of (proc i).

procedure

(make-pvector n v)  pvector?

  n : exact-nonnegative-integer?
  v : any/c
Creates a new persistent vector of length n, with each element filled with the value v.

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.