slices

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AndString

func AndString[T fmt.Stringer](values []T, quote bool) string

AndString is a function that returns a string representation of a slice of strings.

Parameters:

  • values: The values to convert to a string.
  • quote: Whether to quote the values.

Returns:

  • string: The string representation of the values.

func EitherOrString

func EitherOrString[T fmt.Stringer](values []T, quote bool) string

EitherOrString is a function that returns a string representation of a slice of elements.

Parameters:

  • values: The elements to convert to a string.
  • quote: True if the elements should be quoted, false otherwise.

Returns:

  • string: The string representation.

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 FindContentIndexes

func FindContentIndexes[T comparable](op_token, cl_token T, tokens []T) (result [2]int, err error)

FindContentIndexes searches for the positions of opening and closing tokens in a slice of strings.

Parameters:

  • op_token: The string that marks the beginning of the content.
  • cl_token: The string that marks the end of the content.
  • tokens: The slice of strings in which to search for the tokens.

Returns:

  • result: An array of two integers representing the start and end indexes of the content.
  • err: Any error that occurred while searching for the tokens.

Errors:

  • *ErrTokenNotFound: If the opening or closing token is not found in the content.
  • *ErrNeverOpened: If the closing token is found without any corresponding opening token.

Behaviors:

  • The first index of the content is inclusive, while the second index is exclusive.
  • This function returns a partial result when errors occur. ([-1, -1] if errors occur before finding the opening token, [index, 0] if the opening token is found but the closing token is not found.

func FindEquals

func FindEquals[T 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 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 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 IndicesOf

func IndicesOf[T comparable](data []T, sep []T, exclude_sep bool) []int

Indices returns the indices of the separator in the data.

Parameters:

  • data: The data.
  • sep: The separator.
  • exclude_sep: Whether the separator is inclusive. If set to true, the indices will point to the character right after the separator. Otherwise, the indices will point to the character right before the separator.

Returns:

  • []int: The indices.

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

func OrString[T fmt.Stringer](values []T, quote, is_negative bool) string

OrString is a function that returns a string representation of a slice of elements.

Parameters:

  • values: The elements to convert to a string.
  • quote: True if the elements should be quoted, false otherwise.
  • is_negative: True if the string should use "nor" instead of "or", false otherwise.

Returns:

  • string: The string representation.

func OrderedUniquefy added in v0.1.2

func OrderedUniquefy[T cmp.Ordered](elems []T) []T

OrderedUniquefy returns a copy of elems without duplicates.

Parameters:

  • elems: The elements to uniquefy.

Returns:

  • []T: The unique elements.

This function also sorts the elements.

func RemoveEmpty

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

func TryInsert[T cmp.Ordered](slc []T, e T) []T

TryInsert is a helper function that inserts an element into a slice only if the element is not already in the slice.

Parameters:

  • slc: The slice to insert into.
  • e: The element to insert.

Returns:

  • []T: The slice with the inserted element.

This function only works if the slice is sorted.

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

type Equaler interface {
	// Equals returns true if the object is equal to the other object.
	//
	// Parameters:
	//   - other: The other object to compare to.
	//
	// Returns:
	//   - bool: True if the objects are equal, false otherwise.
	Equals(other Equaler) bool
}

Equaler is an interface that defines a method to compare two objects of the same type for equality.

type ErrNeverOpened

type ErrNeverOpened struct{}

ErrNeverOpened is a struct that represents an error when a closing token is found without a corresponding opening token.

func NewErrNeverOpened

func NewErrNeverOpened() *ErrNeverOpened

NewErrNeverOpened is a constructor of ErrNeverOpened.

Returns:

  • *ErrNeverOpened: A pointer to the newly created error.

func (*ErrNeverOpened) Error

func (e *ErrNeverOpened) Error() string

Error implements the error interface.

Message:

  • "closing token found without a corresponding opening token".

type ErrTokenNotFound

type ErrTokenNotFound struct {
	// IsOpening is the type of the token (opening or closing).
	IsOpening bool
}

ErrTokenNotFound is a struct that represents an error when a token is not found in the content.

func NewErrTokenNotFound

func NewErrTokenNotFound(is_opening bool) *ErrTokenNotFound

NewErrTokenNotFound is a constructor of ErrTokenNotFound.

Parameters:

  • is_opening: The type of the token (opening or closing).

Returns:

  • *ErrTokenNotFound: A pointer to the newly created error.

func (*ErrTokenNotFound) Error

func (e *ErrTokenNotFound) Error() string

Error implements the error interface.

Message: "{Type} token is not in the content"

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