stack

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 stack implements a persistent stack.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stack

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

Stack is a persistent stack.

Example (Reverse)
s := New(1, 2, 3, 4, 5)
fmt.Println(s)
fmt.Println(s.Reverse())
Output:

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

func Empty

func Empty() *Stack

Empty returns the empty stack.

func From

func From(value interface{}) *Stack

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

*Stack:

Used directly as it is already immutable.

*TStack:

AsPersistent is called on the value and the result used for the stack.

*Vector:

Used directly as the stack as it is already immutable.

*TVector:

AsPersistent is called on the value and the result used for the stack.

[]interface{}:

New is called with the elements.

seq.Sequable:

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

seq.Sequence:

The stack 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.

func New

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

New converts as list of elements to a persistent stack.

func (*Stack) AsTransient

func (s *Stack) AsTransient() *TStack

AsTransient will return a mutable copy on write version of the stack.

func (*Stack) Conj

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

Conj returns a new stack with the element as the top of the stack. Conj implements a generic mechanism for building collections.

func (*Stack) Equal

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

Equal tests if two sets are Equal by comparing the entries of each. Equal implements the Equaler which allows for deep comparisons.

func (*Stack) Find

func (s *Stack) Find(value interface{}) (interface{}, bool)

Find whether the value exists in the stack by walking every value. Returns the value and whether or not it was found.

func (*Stack) Length

func (s *Stack) Length() int

Length returns the number of elements in the stack.

func (*Stack) Pop

func (s *Stack) Pop() *Stack

Pop returns a new stack without the top element

func (*Stack) Push

func (s *Stack) Push(elem interface{}) *Stack

Push returns a new stack with the element as the top of the stack.

func (*Stack) Range

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

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

func(value interface{}) bool:

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

func(value interface{})

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

func(value T) bool:

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

func(value T)

Takes a value of the type of element stored in the stack and
returns if the loop should continue. Useful for homogeneous stacks.
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 (*Stack) Reduce

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

Reduce is a fast mechanism for reducing a Stack. 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 (*Stack) Reverse

func (s *Stack) Reverse() *vector.Vector

Reverse returns the elements of the stack in FIFO order as a vector.

func (*Stack) Seq

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

Seq returns a representation of the stack as a sequence corresponding to the elements of the stack.

func (*Stack) String

func (s *Stack) String() string

String returns a representation of the stack as a string.

func (*Stack) Top

func (s *Stack) Top() interface{}

Top returns the top of the stack

func (*Stack) Transform

func (s *Stack) Transform(actions ...func(*TStack) *TStack) *Stack

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

type TStack

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

func (*TStack) AsPersistent

func (s *TStack) AsPersistent() *Stack

AsPersistent returns the an immutable version of the stack. Any transient operations performed after this will cause a panic.

func (*TStack) Find

func (s *TStack) Find(value interface{}) (interface{}, bool)

Find whether the value exists in the stack by walking every value. Returns the value and whether or not it was found.

func (*TStack) Length

func (s *TStack) Length() int

Length returns the number of elements in the stack.

func (*TStack) Pop

func (s *TStack) Pop() *TStack

Pop removes the top element of the stack. s is returned.

func (*TStack) Push

func (s *TStack) Push(elem interface{}) *TStack

Push places an element at the top of the stack. s is returned

func (*TStack) Range

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

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

func(value interface{}) bool:

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

func(value interface{})

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

func(value T) bool:

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

func(value T)

Takes a value of the type of element stored in the stack and
returns if the loop should continue. Useful for homogeneous stacks.
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 (*TStack) Reduce

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

Reduce is a fast mechanism for reducing a Stack. 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 (*TStack) String

func (s *TStack) String() string

String returns a representation of the stack as a string.

func (*TStack) Top

func (s *TStack) Top() interface{}

Top returns the top element of the stack.

Jump to

Keyboard shortcuts

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