vector

package
v0.0.0-...-8ed0fbd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 3, 2021 License: EPL-1.0 Imports: 3 Imported by: 31

Documentation

Overview

Package vector implements persistent vector.

This is a Go clone of Clojure's PersistentVector type (https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/PersistentVector.java). For an introduction to the internals, see https://hypirion.com/musings/understanding-persistent-vector-pt-1.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

type Iterator interface {
	// Elem returns the element at the current position.
	Elem() interface{}
	// HasElem returns whether the iterator is pointing to an element.
	HasElem() bool
	// Next moves the iterator to the next position.
	Next()
}

Iterator is an iterator over vector elements. It can be used like this:

for it := v.Iterator(); it.HasElem(); it.Next() {
    elem := it.Elem()
    // do something with elem...
}

type Vector

type Vector interface {
	json.Marshaler
	// Len returns the length of the vector.
	Len() int
	// Index returns the i-th element of the vector, if it exists. The second
	// return value indicates whether the element exists.
	Index(i int) (interface{}, bool)
	// Assoc returns an almost identical Vector, with the i-th element
	// replaced. If the index is smaller than 0 or greater than the length of
	// the vector, it returns nil. If the index is equal to the size of the
	// vector, it is equivalent to Cons.
	Assoc(i int, val interface{}) Vector
	// Cons returns an almost identical Vector, with an additional element
	// appended to the end.
	Cons(val interface{}) Vector
	// Pop returns an almost identical Vector, with the last element removed. It
	// returns nil if the vector is already empty.
	Pop() Vector
	// SubVector returns a subvector containing the elements from i up to but
	// not including j.
	SubVector(i, j int) Vector
	// Iterator returns an iterator over the vector.
	Iterator() Iterator
}

Vector is a persistent sequential container for arbitrary values. It supports O(1) lookup by index, modification by index, and insertion and removal operations at the end. Being a persistent variant of the data structure, it is immutable, and provides O(1) operations to create modified versions of the vector that shares the underlying data structure, making it suitable for concurrent access. The empty value is a valid empty vector.

var Empty Vector = &vector{}

Empty is an empty Vector.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL