SliceExt

package
v0.2.38 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoWhile added in v0.2.38

func DoWhile[T any](todo []T, accept PredicateFilter[T], f uc.EvalManyFunc[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 FilterByNegativeWeight

func FilterByNegativeWeight[T any](S []WeightResult[T]) (solution []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:

  • solution: 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 any](S []WeightResult[T]) (solution []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:

  • solution: 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 FindSubsliceFrom added in v0.2.28

func FindSubsliceFrom[T uc.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 FindSubsliceFromFunc added in v0.2.28

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

FindSubsliceFromFunc 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.
  • equals: The comparison function.

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 added in v0.2.24

func IndexOfDuplicate[T uc.Comparable](S []T) int

IndexOfDuplicate returns the index of the first duplicate element in the slice. If there are no duplicates, it returns -1.

Parameters:

  • S: slice of elements.

Returns:

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

Behavior:

  • If there are less than two elements in the slice, the function returns -1.

func IndexOfDuplicateFunc added in v0.2.24

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

IndexOfDuplicateFunc returns the index of the first duplicate element in the slice. If there are no duplicates, it returns -1.

Parameters:

  • S: slice of elements.
  • equals: function that takes two elements and returns a bool.

Returns:

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

Behavior:

  • If there are less than two elements in the slice, the function returns -1.

func RemoveDuplicates added in v0.2.24

func RemoveDuplicates[T uc.Comparable](S []T) []T

RemoveDuplicates removes duplicate elements from the slice.

Parameters:

  • S: slice of elements.

Returns:

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

Behavior:

  • The function preserves the order of the elements in the slice.
  • If there are multiple duplicates of an element, only the first occurrence is kept.
  • If there are less than two elements in the slice, the function returns the original slice.

func RemoveDuplicatesFunc added in v0.2.24

func RemoveDuplicatesFunc[T uc.Equaler[T]](S []T) []T

RemoveDuplicatesFunc removes duplicate elements from the slice.

Parameters:

  • S: slice of elements.
  • equals: function that takes two elements and returns a bool.

Returns:

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

Behavior:

  • The function preserves the order of the elements in the slice.
  • If there are multiple duplicates of an element, only the first occurrence is kept.
  • If there are less than two elements in the slice, the function returns the original slice.

func SFSeparate added in v0.2.21

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 added in v0.2.21

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]) (solution []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.

Types

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 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 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 WeightFunc added in v0.2.32

type WeightFunc[T any] func(elem T) (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 WeightResult added in v0.2.32

type WeightResult[T any] cdp.Pair[T, float64]

WeightResult is a type that represents an element with its corresponding weight.

func ApplyWeightFunc added in v0.2.32

func ApplyWeightFunc[T any](S []T, f WeightFunc[T]) []WeightResult[T]

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[T]: 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 NewWeightResult added in v0.2.32

func NewWeightResult[T any](elem T, weight float64) WeightResult[T]

NewWeightResult creates a new WeightResult with the given weight and element.

Parameters:

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

Returns:

  • WeightResult[T]: The new WeightResult.

Jump to

Keyboard shortcuts

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