Documentation ¶
Index ¶
- func ComplexFilter[T any](slice *[]T, fn func(indices *[]int) bool)
- func Filter[T any](slice *[]T, p Predicate[T])
- func FilterIfApplicable[T any](slice *[]T, p Predicate[T]) bool
- func IndicesOf[T comparable](slice []T, sep T) []int
- func MayInsert[T cmp.Ordered](slice *[]T, elem T) (bool, error)
- func Merge[T cmp.Ordered](dest *[]T, from []T) (int, error)
- func NewErrNotAsExpected[T any](quote bool, kind string, got any, expecteds ...T) error
- func Reject[T any](slice *[]T, p Predicate[T])
- func RejectIfApplicable[T any](slice *[]T, p Predicate[T]) bool
- func RejectNils[T any](slice *[]*T)
- func Split[T any](elems []T, predicate Predicate[T]) ([]T, []T)
- func Uniquefy[T cmp.Ordered](slice *[]T) int
- type Builder
- type Predicate
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ComplexFilter ¶ added in v0.1.1
ComplexFilter applies a filter function on a slice of elements based on the provided filter function. As with Filter, this function modifies the original list in-place.
This function uses indices for optimization reasons.
Parameters:
- slice: The slice of elements to filter.
- fn: The filter function that takes a list of indices and returns a boolean indicating whether to early exit.
Behavior:
- If the provided slice is empty or the filter function is nil, the original slice is cleared and set to nil.
- The filter function is called repeatedly with the current list of indices until it returns true or the list of indices is empty.
- The filtered slice contains only the elements corresponding to the selected indices.
func Filter ¶ added in v0.1.2
Filter applies a predicate function on a slice of elements; keeping only those elements that satisfy the predicate. This function modifies the original list in-place.
Parameters:
- slice: the list of elements to filter.
- p: the predicate function to apply.
Behavior:
- If the list is empty, the predicate is nil, or there is no element that satisfies the predicate, the slice is cleared and set to nil.
func FilterIfApplicable ¶ added in v0.1.4
FilterIfApplicable applies a predicate function on a slice of elements, retaining only those elements that satisfy the predicate. This function modifies the original list in-place. Does nothing if no elements satisfy the predicate.
Parameters:
- slice: The list of elements to filter.
- p: The predicate function to apply.
Returns:
- bool: True if the slice was empty or elements were successfully filtered, false if the predicate is nil or no elements satisfy the predicate.
Behavior:
- If the list is empty or all elements are removed, returns true.
- If the predicate is nil or no elements satisfy the predicate, returns false.
func IndicesOf ¶ added in v0.1.2
func IndicesOf[T comparable](slice []T, sep T) []int
IndicesOf returns a slice of indices that specify where the separator occurs in the data.
Parameters:
- slice: The data.
- sep: The separator.
Returns:
- []int: The indices. Nil if no separator is found.
func MayInsert ¶ added in v0.1.4
MayInsert attempts to insert an element into a sorted slice if it is not already present.
Parameters:
- slice: A pointer to a slice of ordered elements.
- elem: The element to insert.
Returns:
- bool: Returns true if the element was inserted into the slice, false otherwise.
- error: Returns ErrBadParam if slice is nil.
If the element is not found in the slice, it is inserted in the correct position to maintain order.
func Merge ¶ added in v0.1.4
Merge inserts elements from the 'from' slice into the 'dest' slice, maintaining order and ensuring no duplicates.
Parameters:
- dest: A pointer to the destination slice where elements will be inserted. This slice must be sorted and free of duplicates. If not, Uniquefy must be called on it first.
- from: The slice of elements to merge into the destination.
Returns:
- int: The number of elements that were not inserted or could not be merged.
- error: Returns ErrBadParam if dest is nil.
If 'from' is empty, the function does nothing. Each element from 'from' is inserted into 'dest' in the correct position, ensuring that 'dest' remains sorted and free of duplicates.
func NewErrNotAsExpected ¶ added in v0.1.4
NewErrNotAsExpected is a convenience function that creates a new ErrNotAsExpected error with the specified kind, got value, and expected values.
See common.NewErrNotAsExpected for more information.
func Reject ¶ added in v0.1.2
Reject applies a predicate function on a slice of elements; keeping only those elements that do not satisfy the predicate. This function modifies the original list in-place.
Parameters:
- slice: the list of elements to filter.
- p: the predicate function to apply.
Returns:
- []T: the list of elements that do not satisfy the predicate.
Behavior:
- If the list is empty or the predicate is nil, returns nil.
func RejectIfApplicable ¶ added in v0.1.4
RejectIfApplicable applies a predicate function on a slice of elements, rejecting only those elements that do not satisfy the predicate. This function modifies the original list in-place. Does nothing if no elements do not satisfy the predicate.
Parameters:
- slice: the list of elements to filter.
- p: the predicate function to apply.
Returns:
- bool: True if the slice was empty or all elements were successfully filtered, false if the predicate is nil or no elements do not satisfy the predicate.
func RejectNils ¶
func RejectNils[T any](slice *[]*T)
RejectNils works like Reject but keeps only non-nil elements.
Parameters:
- slice: the list of elements to filter.
func Split ¶ added in v0.1.5
Split splits the given slice into two slices.
If the given predicate is nil, the whole slice is returned for the second slice.
If the given slice is empty, both slices will be empty.
The order of the elements in the returned slices is the same as the order in the given slice.
Parameters:
- elems: The elements to split.
- predicate: The predicate to use to determine which elements to keep.
Returns:
- []T: The elements for which the predicate returned true.
- []T: The elements for which the predicate returned false.
func Uniquefy ¶ added in v0.1.4
Uniquefy removes duplicate elements from a slice, in-place, while also sorting the slice in ascending order.
Parameters:
- slice: A pointer to the slice where duplicate elements will be removed from.
Returns:
- int: The number of elements removed from the slice.
The slice is also resized while clearing the removed elements.
Types ¶
type Builder ¶ added in v0.1.1
type Builder[T any] struct { // contains filtered or unexported fields }
Builder is a builder for slices. While this is normally not needed, it can have some uses when making many slices one after the other.
func (*Builder[T]) Append ¶ added in v0.1.1
Append appends an element to the slice being built.
Parameters:
- elem: The element to append.
Returns:
- error: An error if the receiver is nil.