Helpers

package
v0.3.19 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package Helpers provides a set of helper functions and types that can be used for automatic error handling and result evaluation.

However, this is still Work In Progress and is not yet fully implemented.

Package Helpers provides a set of helper functions and types that can be used for automatic error handling and result evaluation.

However, this is still Work In Progress and is not yet fully implemented.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoIfFailure

func DoIfFailure[T Helperer[O], O any](S []T, f uc.DualDoFunc[O, error])

DoIfFailure executes a function for each failed helper.

Parameters:

  • S: slice of helpers.
  • f: the function to execute.

func DoIfSuccess

func DoIfSuccess[T Helperer[O], O any](S []T, f uc.DoFunc[O])

DoIfSuccess executes a function for each successful helper.

Parameters:

  • S: slice of helpers.
  • f: the function to execute.

func ExtractResults

func ExtractResults[T Helperer[O], O any](S []T) []O

ExtractResults extracts the results from the helpers. Unlike with the GetData method, this function returns only the results and not the pair of results and errors.

Parameters:

  • S: slice of helpers.

Returns:

  • []O: slice of results.

Behaviors:

  • The results are returned regardless of whether the helper is successful or not.

func FilterByNegativeWeight

func FilterByNegativeWeight[T Helperer[O], O any](S []T) []T

FilterByNegativeWeight is a function that iterates over weight results and returns the elements with the minimum weight.

Parameters:

  • S: slice of weight results.

Returns:

  • []T: slice of elements with the minimum weight.

Behaviors:

  • If S is empty, the function returns a nil slice.
  • If multiple elements have the same minimum weight, they are all returned.
  • If S contains only one element, that element is returned.

func FilterByPositiveWeight

func FilterByPositiveWeight[T Helperer[O], O any](S []T) []T

FilterByPositiveWeight is a function that iterates over weight results and returns the elements with the maximum weight.

Parameters:

  • S: slice of weight results.

Returns:

  • []T: slice of elements with the maximum weight.

Behaviors:

  • If S is empty, the function returns a nil slice.
  • If multiple elements have the same maximum weight, they are all returned.
  • If S contains only one element, that element is returned.

func FilterIsSuccess added in v0.3.16

func FilterIsSuccess[T Helperer[O], O any](h T) bool

FilterIsSuccess filters any helper that is not successful.

Parameters:

  • h: The helper to filter.

Returns:

  • bool: True if the helper is successful, false otherwise.

Behaviors:

  • It assumes that the h is not nil.

func SuccessOrFail added in v0.3.13

func SuccessOrFail[T Helperer[O], O any](batch []T, useMax bool) ([]T, bool)

SuccessOrFail returns the results with the maximum weight.

Parameters:

  • batch: The slice of results.
  • useMax: True if the maximum weight should be used, false otherwise.

Returns:

  • []*up.Pair[O, error]: The results with the maximum weight.
  • bool: True if the slice was filtered, false otherwise.

Behaviors:

  • If the slice is empty, the function returns a nil slice and true.
  • The result can either be the sucessful results or the original slice. Nonetheless, the maximum weight is always applied.

Types

type Helperer

type Helperer[O any] interface {
	// GetData returns the data of the element.
	//
	// Returns:
	//   - *up.Pair[O, error]: The data of the element.
	GetData() *up.Pair[O, error]

	// GetWeight returns the weight of the element.
	//
	// Returns:
	//   - float64: The weight of the element.
	GetWeight() float64
}

Helper is an interface that represents a helper.

type SimpleHelper

type SimpleHelper[O any] struct {
	// contains filtered or unexported fields
}

SimpleHelper is a type that represents the result of a function evaluation that can either be successful or a failure.

func EvaluateSimpleHelpers added in v0.3.13

func EvaluateSimpleHelpers[T any, O any](batch []T, f uc.EvalOneFunc[T, O]) ([]*SimpleHelper[O], bool)

EvaluateSimpleHelpers is a function that evaluates a batch of helpers and returns the results.

Parameters:

  • batch: The slice of helpers.
  • f: The evaluation function.

Returns:

  • []*SimpleHelper[O]: The results of the evaluation.
  • bool: True if the slice was filtered, false otherwise.

Behaviors:

  • This function returns either the successful results or the original slice.

func NewSimpleHelper

func NewSimpleHelper[O any](result O, reason error) *SimpleHelper[O]

NewSimpleHelper creates a new SimpleHelper with the given result and reason.

Parameters:

  • result: The result of the function evaluation.
  • reason: The error that occurred during the function evaluation.

Returns:

  • SimpleHelper: The new SimpleHelper.

func (*SimpleHelper[O]) GetData

func (h *SimpleHelper[O]) GetData() *up.Pair[O, error]

GetData returns the result of the function evaluation.

Returns:

  • *up.Pair[O, error]: The result of the function evaluation.

func (*SimpleHelper[O]) GetWeight

func (h *SimpleHelper[O]) GetWeight() float64

GetWeight returns the weight of the element.

Returns:

  • float64: The weight of the element.

type WeightFunc

type WeightFunc[O any] func(elem O) (float64, bool)

WeightFunc is a type that defines a function that assigns a weight to an element.

Parameters:

  • elem: The element to assign a weight to.

Returns:

  • float64: The weight of the element.
  • bool: True if the weight is valid, otherwise false.

type WeightedElement

type WeightedElement[O any] struct {
	// contains filtered or unexported fields
}

WeightedElement is a type that represents an element with a weight.

func ApplyWeightFunc

func ApplyWeightFunc[O any](S []O, f WeightFunc[O]) []*WeightedElement[O]

ApplyWeightFunc is a function that iterates over the slice and applies the weight function to each element.

Parameters:

  • S: slice of elements.
  • f: the weight function.

Returns:

  • []WeightResult[O]: slice of elements with their corresponding weights.

Behaviors:

  • If S is empty or f is nil, the function returns nil.
  • If the weight function returns false, the element is not included in the result.

func NewWeightedElement

func NewWeightedElement[O any](elem O, weight float64) *WeightedElement[O]

NewWeightedElement creates a new WeightedElement with the given element and weight.

Parameters:

  • elem: The element.
  • weight: The weight of the element.

Returns:

  • *WeightedElement: The new WeightedElement.

func (*WeightedElement[O]) GetData

func (we *WeightedElement[O]) GetData() *up.Pair[O, error]

GetData returns the data of the element.

Returns:

  • *up.Pair[O, error]: The data of the element.

Behaviors:

  • The second value of the pair is always nil.

func (*WeightedElement[O]) GetWeight

func (we *WeightedElement[O]) GetWeight() float64

GetWeight returns the weight of the element.

Returns:

  • float64: The weight of the element.

type WeightedHelper

type WeightedHelper[O any] struct {
	// contains filtered or unexported fields
}

WeightedHelper is a generic type that represents the result of a function evaluation.

func EvaluateWeightHelpers added in v0.3.13

func EvaluateWeightHelpers[T any, O any](batch []T, f uc.EvalOneFunc[T, O], wf WeightFunc[T], useMax bool) ([]*WeightedHelper[O], bool)

EvaluateWeightHelpers is a function that evaluates a batch of helpers and returns the results.

Parameters:

  • batch: The slice of helpers.
  • f: The evaluation function.
  • wf: The weight function.
  • useMax: True if the maximum weight should be used, false otherwise.

Returns:

  • []*WeightedHelper[O]: The results of the evaluation.
  • bool: True if the slice was filtered, false otherwise.

Behaviors:

  • This function returns either the successful results or the original slice.

func NewWeightedHelper

func NewWeightedHelper[O any](result O, reason error, weight float64) *WeightedHelper[O]

NewWeightedHelper creates a new WeightedHelper with the given result, reason, and weight.

Parameters:

  • result: The result of the function evaluation.
  • reason: The error that occurred during the function evaluation.
  • weight: The weight of the result. The higher the weight, the more likely the result is correct.

Returns:

  • WeightedHelper: The new WeightedHelper.

func (*WeightedHelper[O]) GetData

func (h *WeightedHelper[O]) GetData() *up.Pair[O, error]

GetData returns the result of the function evaluation.

Returns:

  • O: The result of the function evaluation.

func (*WeightedHelper[O]) GetWeight

func (h *WeightedHelper[O]) GetWeight() float64

GetReason returns the error that occurred during the function evaluation.

Returns:

  • error: The error that occurred during the function evaluation.

Jump to

Keyboard shortcuts

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