slice

package
v0.3.38 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2024 License: MIT Imports: 2 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.

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

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 FilterNilValues

func FilterNilValues[T any](S []*T) []*T

FilterNilValues is a function that iterates over the slice and removes the nil elements.

Parameters:

  • S: slice of elements.

Returns:

  • []*T: slice of elements that satisfy the filter function.

Behavior:

  • If S is empty, the function returns a nil slice.

func Find

func Find[T comparable](S []T, elem T) int

Find returns the index of the first occurrence of an element in the slice.

Parameters:

  • S: slice of elements.
  • elem: element to find.

Returns:

  • int: index of the first occurrence of the element or -1 if not found.

func FindEquals

func FindEquals[T uc.Equaler](S []T, elem T) int

FindEquals is the same as Find but uses the Equals method of the elements.

Parameters:

  • S: slice of elements.
  • elem: element to find.

Returns:

  • int: index of the first occurrence of the element or -1 if not found.

func FindSubsliceFrom

func FindSubsliceFrom[T comparable](S []T, subS []T, at int) int

FindSubBytesFrom finds the first occurrence of a subslice in a byte slice starting from a given index.

Parameters:

  • S: The byte slice to search in.
  • subS: The byte slice to search for.
  • at: The index to start searching from.

Returns:

  • int: The index of the first occurrence of the subslice.

Behavior:

  • The function uses the Knuth-Morris-Pratt algorithm to find the subslice.
  • If S or subS is empty, the function returns -1.
  • If the subslice is not found, the function returns -1.
  • If at is negative, it is set to 0.

func FindSubsliceFromEquals

func FindSubsliceFromEquals[T uc.Equaler](S []T, subS []T, at int) int

FindSubsliceFromEquals finds the first occurrence of a subslice in a byte slice starting from a given index using a custom comparison function.

Parameters:

  • S: The byte slice to search in.
  • subS: The byte slice to search for.
  • at: The index to start searching from.

Returns:

  • int: The index of the first occurrence of the subslice.

Behavior:

  • The function uses the Knuth-Morris-Pratt algorithm to find the subslice.
  • If S or subS is empty, the function returns -1.
  • If the subslice is not found, the function returns -1.
  • If at is negative, it is set to 0.

func IndexOfDuplicate

func IndexOfDuplicate[T comparable](S []T) int

IndexOfDuplicate returns the index of the first duplicate element in the slice.

Parameters:

  • S: slice of elements.

Returns:

  • int: index of the first duplicate element or -1 if there are no duplicates.

func IndexOfDuplicateEquals

func IndexOfDuplicateEquals[T uc.Equaler](S []T) int

IndexOfDuplicateEquals is the same as IndexOfDuplicate but uses the Equals method of the elements.

Parameters:

  • S: slice of elements.

Returns:

  • int: index of the first duplicate element or -1 if there are no duplicates.

func MergeUnique

func MergeUnique[T comparable](S1, S2 []T) []T

MergeUnique merges two slices and removes duplicate elements.

Parameters:

  • S1: first slice of elements.
  • S2: second slice of elements.

Returns:

  • []T: slice of elements with duplicates removed.

Behaviors:

  • The function does not preserve the order of the elements in the slices.

func MergeUniqueEquals

func MergeUniqueEquals[T uc.Equaler](S1, S2 []T) []T

MergeUniqueEquals is the same as MergeUnique but uses the Equals method of the elements.

Parameters:

  • S1: first slice of elements.
  • S2: second slice of elements.

Returns:

  • []T: slice of elements with duplicates removed.

Behaviors:

  • The function does preserve the order of the elements in the slices.

func RemoveEmpty added in v0.3.25

func RemoveEmpty[T comparable](elems []T) []T

RemoveEmpty is a function that removes the empty elements from a slice.

Parameters:

  • elems: The slice of elements.

Returns:

  • []T: The slice of elements without the empty elements.

func SFSeparate

func SFSeparate[T any](S []T, filter PredicateFilter[T]) ([]T, []T)

SFSeparate is a function that iterates over the slice and applies the filter function to each element. The returned slices contain the elements that satisfy and do not satisfy the filter function.

Parameters:

  • S: slice of elements.
  • filter: function that takes an element and returns a bool.

Returns:

  • []T: slice of elements that satisfy the filter function.
  • []T: slice of elements that do not satisfy the filter function.

Behavior:

  • If S is empty, the function returns two empty slices.

func SFSeparateEarly

func SFSeparateEarly[T any](S []T, filter PredicateFilter[T]) ([]T, bool)

SFSeparateEarly is a variant of SFSeparate that returns all successful elements. If there are none, it returns the original slice and false.

Parameters:

  • S: slice of elements.
  • filter: function that takes an element and returns a bool.

Returns:

  • []T: slice of elements that satisfy the filter function or the original slice.
  • bool: true if there are successful elements, otherwise false.

Behavior:

  • If S is empty, the function returns an empty slice and true.

func SliceFilter

func SliceFilter[T any](S []T, filter PredicateFilter[T]) []T

SliceFilter is a function that iterates over the slice and applies the filter function to each element.

Parameters:

  • S: slice of elements.
  • filter: function that takes an element and returns a bool.

Returns:

  • []T: slice of elements that satisfy the filter function.

Behavior:

  • If S is empty, the function returns a nil slice.
  • If S has only one element and it satisfies the filter function, the function returns a slice with that element. Otherwise, it returns a nil slice.
  • An element is said to satisfy the filter function if the function returns true when applied to the element.
  • If the filter function is nil, the function returns the original slice.

func SuccessOrFail

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:

  • []*uc.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.

func Uniquefy

func Uniquefy[T comparable](S []T, prioritizeFirst bool) []T

Uniquefy removes duplicate elements from the slice.

Parameters:

  • S: slice of elements.
  • prioritizeFirst: If true, the first occurrence of an element is kept. If false, the last occurrence of an element is kept.

Returns:

  • []T: slice of elements with duplicates removed.

Behavior:

  • The function preserves the order of the elements in the slice.

func UniquefyEquals

func UniquefyEquals[T uc.Equaler](S []T, prioritizeFirst bool) []T

UniquefyEquals is the same as Uniquefy but uses the Equals method of the elements.

Parameters:

  • S: slice of elements.
  • prioritizeFirst: If true, the first occurrence of an element is kept. If false, the last occurrence of an element is kept.

Returns:

  • []T: slice of elements with duplicates removed.

Behavior:

  • The function preserves the order of the elements in the slice.
  • This can modify the original slice.

Types

type Helperer

type Helperer[O any] interface {
	// GetData returns the data of the element.
	//
	// Returns:
	//   - *uc.Pair[O, error]: The data of the element.
	GetData() uc.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 PredicateFilter

type PredicateFilter[T any] func(T) bool

PredicateFilter is a type that defines a slice filter function.

Parameters:

  • T: The type of the elements in the slice.

Returns:

  • bool: True if the element satisfies the filter function, otherwise false.

func FilterNilPredicates

func FilterNilPredicates[T any](S []PredicateFilter[T]) []PredicateFilter[T]

FilterNilPredicates is a function that iterates over the slice and removes the nil predicate functions.

Parameters:

  • S: slice of predicate functions.

Returns:

  • []PredicateFilter: slice of predicate functions that are not nil.

Behavior:

  • If S is empty, the function returns a nil slice.

func Intersect

func Intersect[T any](funcs ...PredicateFilter[T]) PredicateFilter[T]

Intersect returns a PredicateFilter function that checks if an element satisfies all the PredicateFilter functions in funcs.

Parameters:

  • funcs: A slice of PredicateFilter functions.

Returns:

  • PredicateFilter: A PredicateFilter function that checks if a element satisfies all the PredicateFilter functions in funcs.

Behavior:

  • If no filter functions are provided, then all elements are considered to satisfy the filter function.
  • It returns false as soon as it finds a function in funcs that the element does not satisfy.

func ParallelIntersect

func ParallelIntersect[T any](funcs ...PredicateFilter[T]) PredicateFilter[T]

ParallelIntersect returns a PredicateFilter function that checks if an element satisfies all the PredicateFilter functions in funcs concurrently.

Parameters:

  • funcs: A slice of PredicateFilter functions.

Returns:

  • PredicateFilter: A PredicateFilter function that checks if a element satisfies all the PredicateFilter functions in funcs.

Behavior:

  • If no filter functions are provided, then all elements are considered to satisfy the filter function.
  • It returns false as soon as it finds a function in funcs that the element does not satisfy.

func ParallelUnion

func ParallelUnion[T any](funcs ...PredicateFilter[T]) PredicateFilter[T]

ParallelUnion returns a PredicateFilter function that checks if an element satisfies at least one of the PredicateFilter functions in funcs concurrently.

Parameters:

  • funcs: A slice of PredicateFilter functions.

Returns:

  • PredicateFilter: A PredicateFilter function that checks if a element satisfies at least one of the PredicateFilter functions in funcs.

Behavior:

  • If no filter functions are provided, then no elements are considered to satisfy the filter function.
  • It returns true as soon as it finds a function in funcs that the element satisfies.

func Union

func Union[T any](funcs ...PredicateFilter[T]) PredicateFilter[T]

Union returns a PredicateFilter function that checks if an element satisfies at least one of the PredicateFilter functions in funcs.

Parameters:

  • funcs: A slice of PredicateFilter functions.

Returns:

  • PredicateFilter: A PredicateFilter function that checks if a element satisfies at least one of the PredicateFilter functions in funcs.

Behavior:

  • If no filter functions are provided, then no elements are considered to satisfy the filter function.
  • It returns true as soon as it finds a function in funcs that the element satisfies.

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

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() uc.Pair[O, error]

GetData implements the Helperer interface.

func (*SimpleHelper[O]) GetWeight

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

GetWeight implements the Helperer interface.

Always returns 0.0.

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]) (weighted []*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:

  • weighted: slice of WeightedElement. Nil if S is empty or f is nil.

Behaviors:

  • 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() uc.Pair[O, error]

GetData returns the data of the element.

Returns:

  • *uc.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

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() uc.Pair[O, error]

GetData implements the Helperer interface.

func (*WeightedHelper[O]) GetWeight

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

GetWeight implements the Helperer interface.

Jump to

Keyboard shortcuts

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