Slices

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoWhile

func DoWhile[T any](todo []T, accept slext.PredicateFilter[T], f uc.EvalManyFunc[T, T]) []T

DoWhile performs a do-while loop on a slice of elements.

Parameters:

  • todo: The elements to perform the do-while loop on.
  • accept: The predicate filter to accept elements.
  • f: The evaluation function to perform on the elements.

Returns:

  • []T: The elements that were accepted.

Behaviors:

  • If todo is empty, the function returns nil.
  • If accept is nil, the function returns nil.
  • If f is nil, the function returns the application of accept on todo.
  • The function performs the do-while loop on the elements in todo.

func Evaluate

func Evaluate[A, T, E, R any](elem LeafEvaluable[A, T, E, R], args []A) ([]T, error)

Evaluate performs a leaf evaluation with a loop.

Parameters:

  • le: The evaluable element.
  • args: The arguments.

Returns:

  • []T: The branches.
  • error: An error if the evaluation fails (reserved for panic-level of critical errors).

Behaviors:

  • The function performs a leaf evaluation with a loop.
  • The function returns the branches.
  • If le is nil, the function returns nil.

Types

type Accepter

type Accepter interface {
	// Accept returns true if the accepter accepts the element.
	//
	// Returns:
	//   - bool: True if the accepter accepts the element, false otherwise.
	Accept() bool
}

Accepter is an interface that represents an accepter.

type ErrLastNotFound

type ErrLastNotFound struct{}

ErrLastNotFound is an error type for when the last element is not found.

func NewErrLastNotFound

func NewErrLastNotFound() *ErrLastNotFound

NewErrLastNotFound creates a new ErrLastNotFound.

Returns:

  • *ErrLastNotFound: The new ErrLastNotFound.

func (*ErrLastNotFound) Error

func (e *ErrLastNotFound) Error() string

Error implements the error interface.

It returns the message: "last element not found".

type FilterNextsFunc

type FilterNextsFunc[T any] func(wasDone bool, nexts []T) ([]T, error)

FilterNextsFunc is a function that filters the next elements.

Parameters:

  • wasDone: If the last element was done.
  • nexts: The next elements.

Returns:

  • []T: The filtered next elements.
  • error: An error if the next elements could not be filtered.

type FrontierEvaluator

type FrontierEvaluator[T Accepter] struct {
	// contains filtered or unexported fields
}

FrontierEvaluator is a type that represents a frontier evaluator.

func NewFrontierEvaluator

func NewFrontierEvaluator[T Accepter](matcher uc.EvalManyFunc[T, T]) *FrontierEvaluator[T]

NewFrontierEvaluator creates a new frontier evaluator.

Parameters:

  • matcher: The matcher.

Returns:

  • *FrontierEvaluator: The new frontier evaluator.

Behaviors:

  • If matcher is nil, then the frontier evaluator will return nil for any evaluation.

func (*FrontierEvaluator[T]) Evaluate

func (fe *FrontierEvaluator[T]) Evaluate(elem T)

Evaluate evaluates the frontier evaluator given an element.

Parameters:

  • elem: The element to evaluate.

Behaviors:

  • If the element is accepted, the solutions will be set to the element.
  • If the element is not accepted, the solutions will be set to the results of the matcher.
  • If the matcher returns an error, the solutions will be set to the error.
  • The evaluations assume that, the more the element is elaborated, the more the weight increases. Thus, it is assumed to be the most likely solution as it is the most elaborated. Euristic: Depth.

func (*FrontierEvaluator[T]) GetResults

func (fe *FrontierEvaluator[T]) GetResults() ([]T, error)

GetResults gets the results of the frontier evaluator.

Returns:

  • []T: The results of the frontier evaluator.
  • error: An error if the frontier evaluator failed.

Behaviors:

  • If the solutions are empty, the function returns nil.
  • If the solutions contain errors, the function returns the first error.
  • Otherwise, the function returns the solutions.

type Laster

type Laster[T any] interface {
	// From creates a new Laster from a slice of elements.
	//
	// Parameters:
	//   - elems: The elements to add to the Laster.
	//
	// Returns:
	//   - Laster[T]: The new Laster.
	//   - error: An error if the elements could not be added.
	From(elems []T) (Laster[T], error)

	// GetLast gets the last element from the Laster.
	//
	// Returns:
	//   - T: The last element.
	//   - bool: If the last element was found.
	GetLast() (T, bool)

	// Append appends an element to the Laster.
	//
	// Parameters:
	//   - elem: The element to append.
	Append(elem T)

	uc.Copier
}

Laster is an interface for a stack of elements.

type LeafEvaluable

type LeafEvaluable[A, T, E, R any] interface {
	// Evaluator is a function type that returns the leaf evaluator.
	//
	// Returns:
	//   - LeafEvaluater[A, T, E, R]: The leaf evaluator.
	Evaluator() LeafEvaluater[A, T, E, R]
}

LeafEvaluable is an interface that represents a leaf evaluable.

type LeafEvaluater

type LeafEvaluater[A, T, E, R any] interface {
	// Init is a function type that initializes the memory and the first branch.
	//
	// Parameters:
	//   - elems: The elements.
	//
	// Returns:
	//   - T: The first branch.
	//   - error: An error if the initialization fails.
	Init(elems []A) (T, error)

	// Core is a function type that performs the core evaluation.
	//
	// Parameters:
	//   - index: The index of the loop element.
	//   - lpe: The loop element.
	//
	// Returns:
	//   - *uc.Pair[R, error]: The result and an error.
	//   - error: An error if the evaluation fails (reserved for panic-level of critical errors).
	Core(index int, lpe E) (*uc.Pair[R, error], error)

	// Next is a function type that performs the next evaluation.
	//
	// Parameters:
	//   - pair: The result and an error.
	//   - branch: The current branch.
	//
	// Returns:
	//   - []T: The new branches.
	//   - error: An error if the evaluation fails (reserved for panic-level of critical errors).
	Next(pair *uc.Pair[R, error], branch T) ([]T, error)

	uc.Iterable[E]
}

LeafEvaluater is an interface that represents a leaf evaluater.

type NextsFunc

type NextsFunc[T any] func(wasDone bool, last T) ([]T, error)

NextsFunc is a function that gets the next elements.

Parameters:

  • wasDone: If the last element was done.
  • last: The last element.

Returns:

  • []T: The next elements.
  • error: An error if the next elements could not be found.

type StackEvaluator

type StackEvaluator[T any, E Laster[T]] struct {
	// contains filtered or unexported fields
}

StackEvaluator evaluates a stack of elements.

func NewStackEvaluator

func NewStackEvaluator[T any, E Laster[T]](eval uc.EvalOneFunc[T, bool], nexts NextsFunc[T]) (*StackEvaluator[T, E], error)

NewStackEvaluator creates a new StackEvaluator.

Parameters:

  • eval: The evaluation function.
  • nexts: The function to get the next elements.

Returns:

  • *StackEvaluator[T, E]: The new StackEvaluator.
  • error: An error of type *errors.ErrInvalidParameter if the eval or nexts functions are nil.

Behaviors:

  • By default, the filter function will return the nexts if the last element was not done. If the last element was done, it will return nil.

func (*StackEvaluator[T, E]) Evaluate

func (se *StackEvaluator[T, E]) Evaluate(elem T) ([]E, error)

Evaluate evaluates the stack of elements from the given element.

Parameters:

  • elem: The element to start the evaluation.

Returns:

  • []E: The evaluated elements.
  • error: An error if the elements could not be evaluated.

func (*StackEvaluator[T, E]) SetFilter

func (se *StackEvaluator[T, E]) SetFilter(filter FilterNextsFunc[T])

SetFilter sets the filter function.

Parameters:

  • filter: The filter function.

Behaviors:

  • If the filter function is nil, the normal filter function will be used.

Jump to

Keyboard shortcuts

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