Slices

package
v0.3.13 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 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 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 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.

Jump to

Keyboard shortcuts

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