SliceExt

package
v0.2.34 Latest Latest
Warning

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

Go to latest
Published: May 4, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FilterByNegativeWeight

func FilterByNegativeWeight[T any](S []WeightResult[T]) []T

FilterByNegativeWeight is a function that iterates over weight results and returns the elements with the minimum weight. If multiple elements have the same minimum weight, they are all returned.

If S is empty, the function returns an empty slice.

Parameters:

  • S: slice of weight results.

Returns:

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

func FilterByPositiveWeight

func FilterByPositiveWeight[T any](S []WeightResult[T]) []T

FilterByPositiveWeight is a function that iterates over weight results and returns the elements with the maximum weight. If multiple elements have the same maximum weight, they are all returned.

If S is empty, the function returns an empty slice.

Parameters:

  • S: slice of weight results.

Returns:

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

func FindSubsliceFrom added in v0.2.28

func FindSubsliceFrom[T intf.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 intf.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 intf.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 intf.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 intf.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 intf.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]) []T

SliceFilter is a function that iterates over the slice and applies the filter function to each element. The returned slice contains the elements that 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.

Behavior:

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

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 all 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] struct {
	// Elem is the element.
	Elem T

	// Weight is the weight of the element.
	Weight 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. The returned slice contains the elements with their corresponding weights but only if the weight is valid.

If S is empty, the function returns an empty slice.

Parameters:

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

Returns:

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

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