Documentation
¶
Index ¶
- func All[I any, S ~[]I](slice S, predicate func(I) bool) bool
- func AllSeq[I any](inputSeq iter.Seq[I], allFunc func(I) bool) bool
- func Any[I any, S ~[]I](slice S, predicate func(I) bool) bool
- func AnySeq[I any](inputSeq iter.Seq[I], anyFunc func(I) bool) bool
- func Chunk[I any, S ~[]I](slice S, chunkSize int) []S
- func Compare[I comparable, S ~[]I](a, b S) bool
- func CompareSeq[I comparable](a, b iter.Seq[I]) bool
- func Contains[I comparable, S ~[]I](slice S, element I) bool
- func ContainsSeq[I comparable](inputSeq iter.Seq[I], element I) bool
- func Difference[I comparable, S ~[]I](a, b S) S
- func DifferenceSeq[I comparable](a, b iter.Seq[I]) iter.Seq[I]
- func Distinct[I comparable, S ~[]I](slice S) S
- func DistinctSeq[I comparable](inputSeq iter.Seq[I]) iter.Seq[I]
- func Ennumerate[I any](inputSeq iter.Seq[I]) iter.Seq2[int, I]
- func ExpandSeq[I any, O any](inputSeq iter.Seq[I], reduceFunc func(I) []O) iter.Seq[O]
- func Filter[I any, S ~[]I](inputSlice S, filterFunc func(I) bool) S
- func FilterSeq[I any](inputSeq iter.Seq[I], filterFunc func(I) bool) iter.Seq[I]
- func Find[I any, S ~[]I](inputSlice S, findFunc func(I) bool) (foundItem I, didFind bool)
- func FindIndex[I any, S ~[]I](inputSlice S, findFunc func(I) bool) int
- func FindIndexSeq[I any](inputSeq iter.Seq[I], findFunc func(I) bool) (foundIndex int, didFind bool)
- func FindSeq[I any](inputSeq iter.Seq[I], findFunc func(I) bool) (foundItem I, didFind bool)
- func GroupBySeq[I any, K comparable](inputSeq iter.Seq[I], keyFunc func(I) K) iter.Seq2[K, iter.Seq[I]]
- func Intersection[I comparable, S ~[]I](a, b S) S
- func IntersectionSeq[I comparable](inputSeq1, inputSeq2 iter.Seq[I]) iter.Seq[I]
- func Map[I any, O any, S ~[]I](inputSlice S, mapFunc func(I) O) []O
- func MapSeq[I any, O any](inputSeq iter.Seq[I], mapFunc func(I) O) iter.Seq[O]
- func Max[T cmp.Ordered](elements ...T) T
- func MaxFunc[T any](max func(T, T) T, elements ...T) T
- func MaxSeq[I cmp.Ordered](inputSeq iter.Seq[I]) I
- func MaxSeqFunc[I any](inputSeq iter.Seq[I], maxFunc func(I, I) I) I
- func ParallelForEach[I any, S ~[]I](inputSlice S, forEachFunc func(I))
- func ParallelMap[I any, O any, S ~[]I](inputSlice S, mapFunc func(I) O) []O
- func Reduce[I any, O any, S ~[]I](inputSlice S, reduceFunc func(O, I) O, initialValue O) O
- func ReduceSeq[I any, O any](inputSeq iter.Seq[I], reduceFunc func(O, I) O, initialValue O) O
- func RemoveElement[I comparable, S ~[]I](slice S, element I, occurrencesToDelete int) S
- func RemoveElementSeq[I comparable](inputSeq iter.Seq[I], element I, occurrencesToDelete int) iter.Seq[I]
- func RemoveElements[I comparable, S ~[]I](slice S, elements ...I) S
- func RemoveElementsSeq[I comparable](inputSeq iter.Seq[I], elements ...I) iter.Seq[I]
- func RemoveFirstOccurrence[I comparable, S ~[]I](slice S, element I) S
- func Reverse[I any, S ~[]I](slice S) S
- func SafeExcecute[T_out any](fn func() (T_out, error)) (output T_out, err error)
- func SafeExcecuteWithStackTrace[T_out any](fn func() (T_out, error)) (output T_out, err error)
- func SafeFind[I any, S ~[]I](inputSlice S, findFunc func(I) (bool, error)) (foundItem I, didFind bool, err error)
- func SafeFindSeq[I any](inputSeq iter.Seq[I], findFunc func(I) (bool, error)) (foundItem I, didFind bool, err error)
- func SafeMap[I any, O any, S ~[]I](inputSlice S, mappingFunc func(I) (O, error)) ([]O, error)
- func SafeMapSeq[I any, O any](inputSeq iter.Seq[I], mapFunc func(I) (O, error)) iter.Seq[O]
- func SafeReduce[I any, O any, S ~[]I](inputSlice S, reduceFunc func(O, I) (O, error), initialValue O) (O, error)
- func SafeReduceSeq[I any, O any](inputSeq iter.Seq[I], reduceFunc func(O, I) (O, error), initialValue O) (O, error)
- func Sort[I any, S ~[]I](slice S, less func(i, j I) bool) S
- func Union[I comparable, S ~[]I](a, b S) S
- func UnionSeq[I comparable](inputSeq1, inputSeq2 iter.Seq[I]) iter.Seq[I]
- func UniqueItemsById[Id comparable, I identifiable[Id], S ~[]I](slice S) S
- func WeightedSort[I any, W cmp.Ordered, S ~[]I](slice S, getWeighfn func(I) W, less func(i, j I) bool) S
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All checks if all elements in the given slice satisfy the provided predicate function. It returns true if all elements satisfy the predicate, otherwise it returns false.
func Any ¶
Any checks if any element in the slice satisfies the given predicate function. It returns true if at least one element matches the predicate, otherwise false.
func Chunk ¶
Chunk splits a slice into multiple smaller slices (chunks) of a specified size. If the chunkSize is less than or equal to 0, or if the input slice is empty, it returns an empty slice of slices.
The function uses generics to work with slices of any type.
Parameters:
- slice: The input slice to be split into chunks.
- chunkSize: The desired size of each chunk.
Returns:
A slice of slices, where each inner slice is a chunk of the original slice.
func Compare ¶
func Compare[I comparable, S ~[]I](a, b S) bool
Compare takes two slices of any comparable type and returns true if they are equal. Two slices are considered equal if they have the same length and all corresponding elements are equal.
Returns true if the slices are equal, false otherwise.
func CompareSeq ¶ added in v0.2.0
func CompareSeq[I comparable](a, b iter.Seq[I]) bool
func Contains ¶
func Contains[I comparable, S ~[]I](slice S, element I) bool
Contains checks if the given element is present in the slice. It returns true if the element is found, otherwise it returns false.
func ContainsSeq ¶ added in v0.2.0
func ContainsSeq[I comparable](inputSeq iter.Seq[I], element I) bool
func Difference ¶
func Difference[I comparable, S ~[]I](a, b S) S
Difference returns the elements in slice `a` that are not in slice `b`. It uses a map to track the elements in `b` for efficient lookups.
Type Parameters:
T: a type that is comparable (supports the == and != operators).
Parameters:
a: the first slice of elements. b: the second slice of elements.
Returns:
A slice containing the elements that are in `a` but not in `b`.
func DifferenceSeq ¶ added in v0.2.0
func DifferenceSeq[I comparable](a, b iter.Seq[I]) iter.Seq[I]
func Distinct ¶
func Distinct[I comparable, S ~[]I](slice S) S
Distinct returns a new slice containing only the distinct elements from the input slice. The order of elements in the result slice is the same as their first occurrence in the input slice.
func DistinctSeq ¶ added in v0.2.0
func DistinctSeq[I comparable](inputSeq iter.Seq[I]) iter.Seq[I]
func ExpandSeq ¶ added in v0.4.0
ExpandSeq takes an input sequence of type iter.Seq[I] and a reduce function that transforms each element of type I into a slice of elements of type O. It returns a new sequence of type iter.Seq[O] where each element of the input sequence is expanded into multiple elements based on the reduce function.
func Filter ¶
Filter applies a filter function to each element in the inputSlice and returns a new slice containing only the elements for which the filter function returns true. The filter function takes an element of type T as input and returns a boolean value. The inputSlice is not modified.
func Find ¶
Find searches for an element in the inputSlice that satisfies the given findFunc. It returns the first element that matches the condition or the zero value of type T if no match is found.
func FindIndex ¶
FindIndex returns the index of the first element in the inputSlice that satisfies the findFunc condition. If no element satisfies the condition, it returns -1.
func FindIndexSeq ¶ added in v0.2.0
func GroupBySeq ¶ added in v0.3.0
func Intersection ¶
func Intersection[I comparable, S ~[]I](a, b S) S
Intersection returns the common elements between two slices. It takes two slices of any comparable type and returns a slice containing the elements that are present in both input slices.
func IntersectionSeq ¶ added in v0.2.0
func IntersectionSeq[I comparable](inputSeq1, inputSeq2 iter.Seq[I]) iter.Seq[I]
func Map ¶
Map applies a mapping function to each element of the input slice and returns a new slice containing the results.
func Max ¶
Max returns the maximum value in the provided slice. If no elements are provided, it panics with "No element provided to Max".
func MaxSeqFunc ¶ added in v0.2.0
func ParallelForEach ¶
func ParallelForEach[I any, S ~[]I](inputSlice S, forEachFunc func(I))
ParallelForEach applies a given function to each element of the input slice in parallel. The number of parallel workers is determined by the minimum of the number of CPU cores and the length of the input slice.
Type Parameters:
T: The type of elements in the input slice.
Parameters:
inputSlice: The slice of elements to process. If nil, the function returns immediately. forEachFunc: The function to apply to each element of the input slice.
Example usage:
ParallelForEach([]int{1, 2, 3, 4}, func(n int) {
fmt.Println(n)
})
func ParallelMap ¶
ParallelMap applies the given map function concurrently to each element in the input slice. It creates a fixed number of worker goroutines to process the elements in parallel. The input slice is divided into chunks and each chunk is processed by a worker goroutine. The results are collected and returned as a new slice in the same order as the input. The map function takes an element of type T as input and returns an element of type U. The number of worker goroutines is determined by the number of available CPU cores. This function blocks until all worker goroutines have completed their tasks.
func Reduce ¶
Reduce applies a function to each element of the input slice and returns a single value. The reduceFunc function takes two arguments: an accumulator value of type U and an element of the input slice of type I. It returns a new accumulator value of type O. The initialValue is the initial value of the accumulator. Reduce iterates over the inputSlice, applying the reduceFunc function to each element and the current accumulator value. The result of each iteration becomes the new accumulator value for the next iteration. Finally, the function returns the final accumulator value.
func RemoveElement ¶
func RemoveElement[I comparable, S ~[]I](slice S, element I, occurrencesToDelete int) S
RemoveElement returns a slice that contains the elements of the input slice with at most n occurrences of element removed.
n == 0 → nothing is removed n > 0 → remove the first n matches, then pass the rest through n == -1 → remove ALL matches
func RemoveElementSeq ¶ added in v0.2.0
func RemoveElementSeq[I comparable](inputSeq iter.Seq[I], element I, occurrencesToDelete int) iter.Seq[I]
RemoveElementSeq returns a sequence that yields the elements of inputSeq with at most n occurrences of element removed.
n == 0 → nothing is removed n > 0 → remove the first n matches, then pass the rest through n == -1 → remove ALL matches
The consumer can stop early by returning false from yield.
func RemoveElements ¶
func RemoveElements[I comparable, S ~[]I](slice S, elements ...I) S
RemoveElements removes all occurrences of the specified elements from the given slice. It returns a new slice with the elements removed.
The function uses a map to keep track of the elements to be removed, ensuring efficient lookups.
func RemoveElementsSeq ¶ added in v0.2.0
func RemoveElementsSeq[I comparable](inputSeq iter.Seq[I], elements ...I) iter.Seq[I]
func RemoveFirstOccurrence ¶
func RemoveFirstOccurrence[I comparable, S ~[]I](slice S, element I) S
RemoveFirstOccurrence removes the first occurrence of the specified element from the given slice. It's a shorthand for calling RemoveElement with occurrencesToDelete set to 1.
func SafeExcecute ¶
SafeExcecute executes a given function and recovers from any panic that occurs during its execution. It returns the output of the function and any error that occurred. If a panic occurs, it intercepts the panic and returns it as an error.
func SafeExcecuteWithStackTrace ¶ added in v0.1.3
SafeExcecuteWithStackTrace executes a function that returns a value and an error, and ensures that any panic during the execution is recovered and converted into an error with a stack trace.
func SafeFind ¶
func SafeFind[I any, S ~[]I](inputSlice S, findFunc func(I) (bool, error)) (foundItem I, didFind bool, err error)
SafeFind iterates over the elements of the input slice and applies the provided findFunc to each element to determine if it matches a specific condition. If a match is found, the function returns the matching item, a boolean indicating success, and nil for the error. If no match is found, it returns the zero value of the item type, false, and nil for the error. If an error occurs during the execution of findFunc, the function immediately returns the zero value of the item type, false, and the encountered error.
func SafeFindSeq ¶ added in v0.2.0
func SafeMap ¶
SafeMap applies a mapping function to each element of an input slice, returning a new slice with the results. If the mapping function returns an error for any element or panics, SafeMap will return that error and halt further processing.
func SafeMapSeq ¶ added in v0.2.0
func SafeReduce ¶
func SafeReduce[I any, O any, S ~[]I](inputSlice S, reduceFunc func(O, I) (O, error), initialValue O) (O, error)
SafeReduce is a generic function that safely reduces a slice of input elements into a single output value by applying a user-defined reduce function. It ensures that if an error is encountered during the reduction process, the reduce stops and returns the error.
func SafeReduceSeq ¶ added in v0.2.0
func Sort ¶
Sort sorts a slice of any type in place based on the provided less function. The less function should return true if the first argument is considered to be less than the second.
func Union ¶
func Union[I comparable, S ~[]I](a, b S) S
Union returns the union of two slices, removing duplicate elements. The function takes two slices of any comparable type and returns a new slice containing all unique elements from both input slices.
Example usage:
a := []int{1, 2, 3}
b := []int{3, 4, 5}
result := Union(a, b) // result will be []int{1, 2, 3, 4, 5}
The order of elements in the resulting slice is not guaranteed.
func UnionSeq ¶ added in v0.2.0
func UnionSeq[I comparable](inputSeq1, inputSeq2 iter.Seq[I]) iter.Seq[I]
func UniqueItemsById ¶
func UniqueItemsById[Id comparable, I identifiable[Id], S ~[]I](slice S) S
UniqueItemsById returns a slice containing only the unique items from the input slice, where uniqueness is determined by the item's Id. The function uses a map to track seen Ids and filters out duplicates. Items should implement the identifiable interface. Thus they must have a method Id() that returns a unique identifier.
func WeightedSort ¶
func WeightedSort[I any, W cmp.Ordered, S ~[]I](slice S, getWeighfn func(I) W, less func(i, j I) bool) S
WeightedSort sorts a slice of any type based on a weight function and a less function. The weight function determines the primary sorting order by returning an integer weight for each element. The less function is used as a secondary sorting order when two elements have the same weight.
Types ¶
This section is empty.