listlike

package
v0.0.0-...-84d6781 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyStack occurs when a pop or peek operation is called on an empty stack.
	// This error can be checked with the == operator.
	//
	// Format:
	// 	"empty stack"
	ErrEmptyStack error

	// ErrEmptyQueue occurs when a pop or peek operation is called on an empty queue.
	// This error can be checked with the == operator.
	//
	// Format:
	// 	"empty queue"
	ErrEmptyQueue error

	// ErrCannotPush occurs when a push operation is called on a refusable stack that
	// was not accepted nor refused yet. This error can be checked with the == operator.
	//
	// Format:
	// 	"cannot push elements: stack not accepted nor refused"
	ErrCannotPush error
)
View Source
var (
	ErrNoAutomaton      error
	ErrBadlyImplemented error
)
View Source
var SD listlikeT

SD is the namespace for SD-like functions.

View Source
var Stack stackT

Functions

func CALL

func CALL[O any](aut Aut[O], arg any) O

CALL executes the given automaton with the provided argument and returns its result. If the automaton reaches a terminal state, it resets and tries again. If the automaton is implemented incorrectly, it panics.

Parameters:

  • aut: The automaton to execute.
  • arg: The argument to pass to the automaton.

Returns:

  • O: The result of the automaton execution.

Panics:

  • ErrNoAutomaton: If the provided automaton is nil.
  • ErrBadlyImplemented: If the automaton reaches a terminal state on the second call.
  • error: If the automaton has an error.

func EACH

func EACH[O any](aut Aut[O], arg any) iter.Seq[O]

func ERROR

func ERROR[O any](aut Aut[O]) error

ERROR checks if the given automaton has an error and returns it.

Parameters:

  • aut: The automaton to check for an error.

Returns:

  • error: The error associated with the automaton if it has one, or nil if there is no error.

Panics:

  • ErrNoAutomaton: If the provided automaton is nil.

func IsEmpty

func IsEmpty(ll any) bool

func Reset

func Reset(ll any)

func TRY

func TRY[O any](aut Aut[O], arg any, handle func(err error) O) O

TRY calls the given automaton with the given argument and returns its result if there is no error. If there is an error, it calls the given error handler with the error and returns its result. If the error handler is nil, it will panic with the error.

Parameters:

  • aut: The automaton to call.
  • arg: The argument to pass to the automaton.
  • handle: The error handler to call if there is an error.

Returns:

  • O: The result of either the automaton or the error handler.

Panics:

  • ErrNoAutomaton: If the provided automaton is nil.
  • error: If there is an error and the error handler is nil.

Types

type ArrayStack

type ArrayStack[T any] struct {
	// contains filtered or unexported fields
}

ArrayStack is a simple implementation of a stack. An empty stack can either be created with the `var stack ArrayStack[T]` syntax or with the `new(ArrayStack[T])` constructor.

func NewStack

func NewStack[T any](elems []T) *ArrayStack[T]

NewStack creates a new stack from a slice.

Parameters:

  • elems: The elements to add to the stack.

Returns:

  • *Stack[T]: The new stack. Never returns nil.

WARNING: As a side-effect, the original list will be reversed.

func (ArrayStack[T]) IsEmpty

func (s ArrayStack[T]) IsEmpty() bool

IsEmpty implements the Lister interface.

func (ArrayStack[T]) Peek

func (s ArrayStack[T]) Peek() (T, error)

Peek returns the element at the top of the stack.

Returns:

  • T: The element at the top of the stack.
  • error: An error if the stack is empty.

Errors:

  • ErrEmptyStack: If the stack is empty.

func (*ArrayStack[T]) Pop

func (s *ArrayStack[T]) Pop() (T, error)

Pop removes an element from the stack.

Returns:

  • T: The element that was removed.
  • error: An error if the element could not be removed from the stack.

Errors:

  • ErrEmptyStack: If the stack is empty.

func (*ArrayStack[T]) Push

func (s *ArrayStack[T]) Push(elem T) error

Push adds an element to the stack.

Parameters:

  • elem: The element to add.

Returns:

  • error: An error if the receiver is nil.

func (*ArrayStack[T]) PushMany

func (s *ArrayStack[T]) PushMany(elems []T) error

PushMany adds multiple elements to the stack. If it has at least one element but the receiver is nil, an error is returned.

Parameters:

  • elems: The elements to add.

Returns:

  • error: An error if the receiver is nil.

WARNING: As a side-effect, the original list will be reversed.

func (*ArrayStack[T]) Reset

func (s *ArrayStack[T]) Reset()

Reset implements the Lister interface.

func (ArrayStack[T]) Size

func (s ArrayStack[T]) Size() int

Size implements the Lister interface.

type Aut

type Aut[O any] interface {
	HasError() bool
	GetError() error
	Call(arg any) bool
	Reset()
	Result() O
}

type Lister

type Lister interface {
	// Size returns the number of elements in the list-like data structure.
	//
	// Returns:
	//   - int: The number of elements in the list-like data structure. Never negative.
	Size() int

	// IsEmpty checks whether the list-like data structure is empty.
	//
	// Returns:
	//   - bool: True if the list-like data structure is empty, false otherwise.
	IsEmpty() bool

	// Reset resets the list-like data structure for reuse.
	Reset()
}

Lister is an interface that can be used by list-like data structures.

func New

func New[T any](elems ...T) Lister

New creates a new generic list-like data structure.

Parameters:

  • elems: The elements to add to the list-like data structure.

Returns:

  • Lister: The new list-like data structure. Never returns nil.

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}

Queue is a simple implementation of a queue. An empty queue can either be created with the `var queue Queue[T]` syntax or with the `new(Queue[T])`

func NewQueue

func NewQueue[T any](elems []T) *Queue[T]

NewQueue creates a new queue from a slice.

Parameters:

  • elems: The elements to add to the queue.

Returns:

  • *Queue[T]: The new queue. Never returns nil.

func (*Queue[T]) Dequeue

func (q *Queue[T]) Dequeue() (T, error)

Dequeue removes the first element from the queue.

Returns:

  • T: The element that was removed.
  • error: An error if the element could not be removed from the queue.

Errors:

  • ErrEmptyQueue: If the queue is empty.

func (*Queue[T]) Enqueue

func (q *Queue[T]) Enqueue(elem T) error

Enqueue adds an element to the queue.

Parameters:

  • elem: The element to add.

Returns:

  • error: An error if the receiver is nil.

func (*Queue[T]) EnqueueMany

func (q *Queue[T]) EnqueueMany(elems []T) error

EnqueueMany adds multiple elements to the queue. If it has at least one element but the receiver is nil, an error is returned.

Parameters:

  • elems: The elements to add.

Returns:

  • error: An error if the receiver is nil.

func (Queue[T]) First

func (q Queue[T]) First() (T, error)

First returns the element at the start of the queue.

Returns:

  • T: The element at the start of the queue.
  • error: An error if the queue is empty.

Errors:

  • ErrEmptyQueue: If the queue is empty.

func (Queue[T]) IsEmpty

func (q Queue[T]) IsEmpty() bool

IsEmpty implements the Lister interface.

func (*Queue[T]) Reset

func (q *Queue[T]) Reset()

Reset implements the Lister interface.

func (Queue[T]) Size

func (q Queue[T]) Size() int

Size implements the Lister interface.

type RefusableStack

type RefusableStack[T any] struct {
	// contains filtered or unexported fields
}

RefusableStack is a simple implementation of a stack. An empty stack can either be created with the `var stack Stack[T]` syntax or with the `new(Stack[T])` constructor.

func NewRefusableStack

func NewRefusableStack[T any](elems []T) *RefusableStack[T]

NewStack creates a new stack from a slice.

Parameters:

  • elems: The elements to add to the stack.

Returns:

  • *Stack[T]: The new stack. Never returns nil.

WARNING: As a side-effect, the original list will be reversed.

func (*RefusableStack[T]) Accept

func (s *RefusableStack[T]) Accept()

Accept accepts all the elements that were popped. Does nothing if no element was popped.

func (RefusableStack[T]) IsEmpty

func (s RefusableStack[T]) IsEmpty() bool

IsEmpty implements the Lister interface.

func (RefusableStack[T]) Peek

func (s RefusableStack[T]) Peek() (T, error)

Peek returns the element at the top of the stack.

Returns:

  • T: The element at the top of the stack.
  • error: An error if the stack is empty.

Errors:

  • ErrEmptyStack: If the stack is empty.

func (*RefusableStack[T]) Pop

func (s *RefusableStack[T]) Pop() (T, error)

Pop removes an element from the stack.

Returns:

  • T: The element that was removed.
  • error: An error if the element could not be removed from the stack.

Errors:

  • ErrEmptyStack: If the stack is empty.

func (RefusableStack[T]) Popped

func (s RefusableStack[T]) Popped() []T

Popped returns the elements that were popped from the stack since the last Accept or Refuse operation. The returned slice contains the elements in the order they were popped, with the most recently popped element at the first position.

Returns:

  • []T: The elements that were popped. Nil if no elements were popped.

func (*RefusableStack[T]) Push

func (s *RefusableStack[T]) Push(elem T) error

Push adds an element to the stack.

Parameters:

  • elem: The element to add.

Returns:

  • error: An error if the receiver is nil.

func (*RefusableStack[T]) PushMany

func (s *RefusableStack[T]) PushMany(elems []T) error

PushMany adds multiple elements to the stack. If it has at least one element but the receiver is nil, an error is returned.

Parameters:

  • elems: The elements to add.

Returns:

  • error: An error if the receiver is nil.

WARNING: As a side-effect, the original list will be reversed.

func (*RefusableStack[T]) Refuse

func (s *RefusableStack[T]) Refuse()

Refuse refuses any element that was popped since the last time Accept was called. Does nothing if no element was popped.

func (*RefusableStack[T]) RefuseOne

func (s *RefusableStack[T]) RefuseOne()

RefuseOne refuses the last popped element. Does nothing if no element was popped.

func (*RefusableStack[T]) Reset

func (s *RefusableStack[T]) Reset()

Reset implements the Lister interface.

func (RefusableStack[T]) Size

func (s RefusableStack[T]) Size() int

Size implements the Lister interface.

func (RefusableStack[T]) Validate

func (s RefusableStack[T]) Validate() error

Validate implements the assert.Validater interface.

An instance of RefusableStack is valid if and only if the following conditions are met: - top >= 0 - top <= len(slice)

type SizeFunc

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

func Size

func Size() *SizeFunc

func (*SizeFunc) Call

func (f *SizeFunc) Call(arg any) bool

func (SizeFunc) GetError

func (f SizeFunc) GetError() error

func (SizeFunc) HasError

func (f SizeFunc) HasError() bool

func (*SizeFunc) Reset

func (f *SizeFunc) Reset()

func (SizeFunc) Result

func (f SizeFunc) Result() uint

type Stacker

type Stacker interface {
	Size() int
	IsEmpty() bool
	Reset()
	Push(elem any)
	PushMany(elems []any)
	Pop() (any, error)
	Peek() (any, error)
}

Jump to

Keyboard shortcuts

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