vector

package
v0.0.0-...-7241100 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package vector implements a Radix Balanced trie based vector.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Slice

type Slice struct {
	// contains filtered or unexported fields
}

Slice is a view of an underlying persistent vector. For the most part a Slice shares semantics with a go slice, except that changes do not modify the underlying vector; instead returning a view of a new persistent vector that shares structure with the original vector.

func (*Slice) Append

func (s *Slice) Append(v interface{}) *Slice

Append will extend the vector and associates the value with new last element. This will return a new copy of the immutable vector sharing structure with the original vector.

func (*Slice) Apply

func (s *Slice) Apply(args ...interface{}) interface{}

Apply takes an arbitrary number of arguments and returns the value At the first argument. Apply allows a slice to be called as a function by the 'dyn' library.

func (*Slice) Assoc

func (s *Slice) Assoc(i int, v interface{}) *Slice

Assoc associates the value with the index in an immutable copy of the vector sharing structure with the original vector.

func (*Slice) At

func (s *Slice) At(i int) interface{}

At returns the element at the supplied index. It will panic if out of bounds.

func (*Slice) Conj

func (s *Slice) Conj(elem interface{}) interface{}

Conj will extend the vector and associates the value with new last element. Conj implements a generic mechanism for building collections.

func (*Slice) Equal

func (s *Slice) Equal(o interface{}) bool

Equal compares each value of the slice to determine if the slice is equal to the one passed in.

func (*Slice) Find

func (s *Slice) Find(idx interface{}) (interface{}, bool)

Find returns the value at the supplied index and if that index was in bounds for the vector. Out of bounds access does not panic but returns (nil, false). idx must be an int.

func (*Slice) Length

func (s *Slice) Length() int

Length returns the number of elements in the vector.

func (*Slice) Range

func (s *Slice) Range(do interface{})

Range calls the passed in function on each element of the slice. The function passed in may be of many types:

func(index int, value interface{}) bool:

Takes the index and a value of any type and returns if the loop should continue.
Useful to avoid reflection where not needed and to support
heterogenous slices.

func(index int, value interface{})

Takes the index and a value of any type.
Useful to avoid reflection where not needed and to support
heterogenous slices.

func(index int, value T) bool:

Takes the index and a value of the type of element stored in the slice and
returns if the loop should continue. Useful for homogeneous slices.
Is called with reflection and will panic if the type is incorrect.

func(index int, value T)

Takes the index and a value of the type of element stored in the slice and
returns if the loop should continue. Useful for homogeneous slices.
Is called with reflection and will panic if the type is incorrect.

Range will panic if passed anything that doesn't match one of these signatures

func (*Slice) Reduce

func (s *Slice) Reduce(fn interface{}, init interface{}) interface{}

Reduce is a fast mechanism for reducing a Vector. Reduce can take the following types as the fn:

func(init interface{}, value interface{}) interface{} func(init iT, v vT) oT

Reduce will panic if given any other function type.

func (*Slice) Seq

func (s *Slice) Seq() seq.Sequence

Seq returns a seq.Sequence that will traverse the vector.

func (*Slice) Slice

func (s *Slice) Slice(start, end int) *Slice

Slice will further limit the view of this slice.

func (*Slice) String

func (s *Slice) String() string

String coverts the vector to a string representation.

type TVector

type TVector struct {
	// contains filtered or unexported fields
}

TVector is a transient version of a Vector. Changes made to a transient vector will not effect the original persistent structure. Changes occur as mutation of the transient. The changes made will become immutable when AsPersistent is called. This structure is useful when making mulitple modifications to a persistent vector where the intermediate results will not be seen or stored anywhere.

func (*TVector) Append

func (v *TVector) Append(value interface{}) *TVector

Append will extend the vector and associates the value with new last element. It will panic if called after AsPersistent.

func (*TVector) Apply

func (v *TVector) Apply(args ...interface{}) interface{}

Apply takes an arbitrary number of arguments and returns the value At the first argument. Apply allows vector to be called as a function by the 'dyn' library.

func (*TVector) AsPersistent

func (v *TVector) AsPersistent() *Vector

AsPersistent will transform this transient vector into a persistent vector. Once this occurs any additional actions on the transient vector will panic.

func (*TVector) Assoc

func (v *TVector) Assoc(i int, value interface{}) *TVector

Assoc associates the value with the index. It will panic if called after AsPersistent.

func (*TVector) At

func (v *TVector) At(i int) interface{}

At returns the element at the supplied index. It will panic if out of bounds or called after AsPersistent.

func (*TVector) Conj

func (v *TVector) Conj(elem interface{}) interface{}

Conj will extend the vector and associates the value with new last element. Conj implements a generic mechanism for building collections.

func (*TVector) Delete

func (v *TVector) Delete(idx int) *TVector

Delete removes the element at the current index, shifting the others down and yeilding a vector with one fewer elements.

func (*TVector) Find

func (v *TVector) Find(idx interface{}) (interface{}, bool)

Find returns the value at the supplied index and if that index was in bounds for the vector. Out of bounds access does not panic but returns (nil, false). idx must be an int.

func (*TVector) Insert

func (v *TVector) Insert(idx int, val interface{}) *TVector

Insert adds the value to the vector at the provided index shifting the other values down. This yeilds a vector with an additional value at the provided index.

func (*TVector) Length

func (v *TVector) Length() int

Length returns the number of elements in the vector.

func (*TVector) MakePersistent

func (v *TVector) MakePersistent() interface{}

MakePersistent is a generic version of AsPersistent.

func (*TVector) Pop

func (v *TVector) Pop() *TVector

Pop removes the last element of the vector. It will panic if called after AsPersistent.

func (*TVector) Range

func (v *TVector) Range(do interface{})

Range calls the passed in function on each element of the vector. The function passed in may be of many types:

func(index int, value interface{}) bool:

Takes the index and a value of any type and returns if the loop should continue.
Useful to avoid reflection where not needed and to support
heterogenous vectors.

func(index int, value interface{})

Takes the index and a value of any type.
Useful to avoid reflection where not needed and to support
heterogenous vectors.

func(index int, value T) bool:

Takes the index and a value of the type of element stored in the vector and
returns if the loop should continue. Useful for homogeneous vectors.
Is called with reflection and will panic if the type is incorrect.

func(index int, value T)

Takes the index and a value of the type of element stored in the vector and
returns if the loop should continue. Useful for homogeneous vectors.
Is called with reflection and will panic if the type is incorrect.

Range will panic if passed anything that doesn't match one of these signatures

func (*TVector) Reduce

func (v *TVector) Reduce(fn interface{}, init interface{}) interface{}

Reduce is a fast mechanism for reducing a Vector. Reduce can take the following types as the fn:

func(init interface{}, value interface{}) interface{} func(init iT, v vT) oT

Reduce will panic if given any other function type.

func (*TVector) String

func (v *TVector) String() string

String coverts the vector to a string representation.

type Vector

type Vector struct {
	// contains filtered or unexported fields
}

Vector is a persistent immutable vector. Operations on this structure will return modified copies of the original vector sharing much of the structure with the original.

func Empty

func Empty() *Vector

Empty returns the empty vector

Example
// Empty returns an empty vector. This is
// always the same empty vector.
v := Empty()
fmt.Println(v)
Output:

[]

func From

func From(value interface{}) *Vector

From will convert many go types to an immutable vector. Converting some types is more efficient than others and the mechanisms are described below.

*Vector:

Returned directly as it is already immutable.

*TVector:

AsPersistent is called on the value and the result returned.

[]interface{}:

New is called with the elements.

seq.Sequable:

Seq is called on the value and the vector is built from the resulting sequence.

seq.Sequence:

The vector is built from the sequence. Care should be taken to provide finite sequences or the vector will grow without bound.

[]T:

The slice is converted to a vector using reflection.
Example (Seqable)
// From allows one to create a vectore from a
// seq.Sequable type
l := list.New(1, 2, 3, 4)
v := From(l)
fmt.Println(v)
Output:

[1 2 3 4]
Example (Sequence)
// From allows one to create a vectore from a
// seq.Sequence type
lseq := seq.Seq(list.New(1, 2, 3, 4))
v := From(lseq)
fmt.Println(v)
Output:

[1 2 3 4]
Example (Slice)
// From allows one to create a vectore from a go
// slice.
s := []int{1, 2, 3, 4}
v := From(s)
fmt.Println(v)
Output:

[1 2 3 4]

func New

func New(elems ...interface{}) *Vector

New converts as list of elements to a persistent vector.

Example
// New allows one to create a vector similar to how
// one defines a slice inline in go.
v := New(1, 2, 3, 4)
s := []int{1, 2, 3, 4}
fmt.Println(v)
fmt.Println(s)
Output:

[1 2 3 4]
[1 2 3 4]

func (*Vector) Append

func (v *Vector) Append(value interface{}) *Vector

Append will extend the vector and associates the value with new last element. This will return a new copy of the immutable vector sharing structure with the original vector.

Example
// Append adds a new element to the end of a vector.
// it is equivalent to the go append function.
v := Empty().Append(1)
s := append([]int{}, 1)
fmt.Println(v)
fmt.Println(s)
Output:

[1]
[1]

func (*Vector) Apply

func (v *Vector) Apply(args ...interface{}) interface{}

Apply takes an arbitrary number of arguments and returns the value At the first argument. Apply allows vector to be called as a function by the 'dyn' library.

func (*Vector) AsNative

func (v *Vector) AsNative() []interface{}

AsNative will traverse the vector and return a go native representation of the values contained within.

Example
// AsNative converts the vector to a []interace{}
v := New(1, 2, 3, 4, 5)
s := v.AsNative()
fmt.Printf("%T %v\n", s, s)
Output:

[]interface {} [1 2 3 4 5]

func (*Vector) AsTransient

func (v *Vector) AsTransient() *TVector

AsTransient will return a mutable version of the vector that may be used to perform mutations in a controlled way.

func (*Vector) Assoc

func (v *Vector) Assoc(i int, value interface{}) *Vector

Assoc associates the value with the index in an immutable copy of the vector sharing structure with the original vector.

Example
// Assoc associates a value with an index. This is similar to
// go's s[i] = v operator except that the vector is not modified
// in place.
v := New(1, 2, 3, 4)
v = v.Assoc(0, 10)

s := []int{1, 2, 3, 4}
s[0] = 10

fmt.Println(v)
fmt.Println(s)
Output:

[10 2 3 4]
[10 2 3 4]

func (*Vector) At

func (v *Vector) At(i int) interface{}

At returns the element at the supplied index. It will panic if out of bounds.

Example
// At returns the value at the index. This is similar to go's
// s[i] operator.
v := New(1, 2, 3, 4)
fmt.Println(v.At(2))

s := []int{1, 2, 3, 4}
fmt.Println(s[2])
Output:

3
3

func (*Vector) Conj

func (v *Vector) Conj(elem interface{}) interface{}

Conj will extend the vector and associates the value with new last element. Conj implements a generic mechanism for building collections.

func (*Vector) Delete

func (v *Vector) Delete(idx int) *Vector

Delete removes the element at the current index, shifting the others down and yeilding a vector with one fewer elements.

Example
// Delete removes the item at an index and shifs the other items
// down by one. This is similar to the delete from a slice pattern.
v := New(1, 2, 3, 4)
v = v.Delete(2)
fmt.Println(v)

s := []int{1, 2, 3, 4}
s = append(s[:2], s[3:]...)
fmt.Println(s)
Output:

[1 2 4]
[1 2 4]

func (*Vector) Equal

func (v *Vector) Equal(o interface{}) bool

Equal compares each value of the vector to determine if the vector is equal to the one passed in.

func (*Vector) Find

func (v *Vector) Find(idx interface{}) (interface{}, bool)

Find returns the value at the supplied index and if that index was in bounds for the vector. Out of bounds access does not panic but returns (nil, false). idx must be an int.

func (*Vector) Insert

func (v *Vector) Insert(idx int, val interface{}) *Vector

Insert adds the value to the vector at the provided index shifting the other values down. This yeilds a vector with an additional value at the provided index.

Example
// Insert adds an item at the index and shifts the others up by one.
v := New(1, 2, 3, 4)
v = v.Insert(2, 10)
fmt.Println(v)
Output:

[1 2 10 3 4]

func (*Vector) Length

func (v *Vector) Length() int

Length returns the number of elements in the vector.

Example
// Length returns the length of the vector and is equivalent to
// the go len function.
v := New(1, 2, 3, 4)
s := []int{1, 2, 3, 4}
fmt.Println(v.Length(), len(s))
Output:

4 4

func (*Vector) MakeTransient

func (v *Vector) MakeTransient() interface{}

MakeTransient is a generic version of AsTransient.

func (*Vector) Pop

func (v *Vector) Pop() *Vector

Pop removes the last element of the vector, returning an immutable copy of the vector with one less element, sharing structure with the original vector.

Example
v := New(1, 2, 3, 4)
v = v.Pop()
fmt.Println(v)
Output:

[1 2 3]

func (*Vector) Range

func (v *Vector) Range(do interface{})

Range calls the passed in function on each element of the vector. The function passed in may be of many types:

func(index int, value interface{}) bool:

Takes the index and a value of any type and returns if the loop should continue.
Useful to avoid reflection where not needed and to support
heterogenous vectors.

func(index int, value interface{})

Takes the index and a value of any type.
Useful to avoid reflection where not needed and to support
heterogenous vectors.

func(index int, value T) bool:

Takes the index and a value of the type of element stored in the vector and
returns if the loop should continue. Useful for homogeneous vectors.
Is called with reflection and will panic if the type is incorrect.

func(index int, value T)

Takes the index and a value of the type of element stored in the vector and
returns if the loop should continue. Useful for homogeneous vectors.
Is called with reflection and will panic if the type is incorrect.

Range will panic if passed anything that doesn't match one of these signatures

Example (All)
// Range is a replacement for go's range builtin
// it takes several function forms this version
// will always process all elements.
v := New(1, 2, 3, 4)
v.Range(func(index, value int) {
	fmt.Println(index, value)
})
Output:

0 1
1 2
2 3
3 4
Example (Continue)
// Range is a replacement for go's range builtin
// it takes several function forms this version
// allows one to stop processing by returning false
v := New(1, 2, 3, 4)
v.Range(func(index, value int) bool {
	if value == 3 {
		return false
	}
	fmt.Println(index, value)
	return true
})
Output:

0 1
1 2

func (*Vector) Reduce

func (v *Vector) Reduce(fn interface{}, init interface{}) interface{}

Reduce is a fast mechanism for reducing a Vector. Reduce can take the following types as the fn:

func(init interface{}, value interface{}) interface{} func(init iT, v vT) oT

Reduce will panic if given any other function type.

func (*Vector) Seq

func (v *Vector) Seq() seq.Sequence

Seq returns a seq.Sequence that will traverse the vector.

func (*Vector) Slice

func (v *Vector) Slice(start, end int) *Slice

Slice returns a Slice structure that has the semantics of go slices over the immutable vector.

Example
// Slice allows one to slice a vector this is similar to go's
// slice operators with some caveats. The slice shares structure
// with the original vector but a modification operation on the
// slice will not effect the original vector only the slices'
// backing copy.
v := New(1, 2, 3, 4)
s := v.Slice(1, 4)
fmt.Println(s)
Output:

[2 3 4]

func (*Vector) String

func (v *Vector) String() string

String coverts the vector to a string representation.

func (*Vector) Transform

func (v *Vector) Transform(actions ...func(*TVector) *TVector) *Vector

Transform takes a set of actions and performs them on the persistent vector. It does this by making a transient vector and calling each action on it, then converting it back to a persistent vector.

Example
// Transform allows one to transactionally change a
// vector by going through a transient to make changes
// this allows for faster large changes in a scoped
// way.
v := New(1, 2, 3, 4)
v = v.Transform(func(t *TVector) *TVector {
	return t.Append(5).Append(6).Append(7)
})
fmt.Println(v)
Output:

[1 2 3 4 5 6 7]

Jump to

Keyboard shortcuts

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