Documentation ¶
Index ¶
- Constants
- func Batch(total int64, groups int) [][]int
- func Combine[E any](elements []E) [][]E
- func CombineK[E any](elements []E, k int) [][]E
- func Every[E any](elements []E, predicate PredicateFunc[E]) bool
- func Factorial(n int) int64
- func Filter[E any](elements []E, filter FilterFunc[E]) []E
- func Map[E, V any](elements []E, mapper MapperFunc[E, V]) []V
- func Max[E cmp.Ordered](values ...E) (e E)
- func Min[E cmp.Ordered](values ...E) (e E)
- func NextPermute[E cmp.Ordered](elements []E) (bool, []E)
- func Permutations[E cmp.Ordered](elements []E) (bool, [][]E)
- func PermutationsOrdered[E cmp.Ordered](elements []E) (bool, [][]E)
- func Reduce[E, V any](elements []E, reducer ReducerFunc[E, V], accumulator V) V
- func Repeat[E any](item E, times int) []E
- func Reverse[E any](elements []E) []E
- func Some[E any](elements []E, predicate PredicateFunc[E]) bool
- func Take[E any, T IntTypes](elements []E, takeN int) []E
- func Zip[E any](left, right []E) (bool, [][]E)
- type FilterFunc
- type IntTypes
- type MapperFunc
- type Pair
- type PredicateFunc
- type ReducerFunc
Constants ¶
const MAX_COUNT = 10000000
Variables ¶
This section is empty.
Functions ¶
func Batch ¶
Given total length of array and batch size returns tuple of [startIndex, endIndex] Handles both odd and even Golang truncated division https://go.dev/ref/spec#Arithmetic_operators total = 12 groups = 5 totalGroup = 12/5 + (12 % 5 != 0 ? 1 : 0) = 2 + 1 [0, 4], [5, 9], [10, 11]
total = 12, groups = 3 totalGroups = (12 / 3) + (12 % 3) = 4 [0, 2], [3,5], [6, 8], [9, 11]
func Every ¶
func Every[E any](elements []E, predicate PredicateFunc[E]) bool
Every checks if all elements in the slice meet the condition defined by the provided predicate function. It takes two arguments: - elements: a slice of elements of type E to be checked. - predicate: a function that defines the condition to check for each element. It returns true if all elements satisfy the condition; otherwise, false.
func Filter ¶
func Filter[E any](elements []E, filter FilterFunc[E]) []E
Filter applies a filter function to each element in a slice and returns a new slice of elements that meet the condition. It takes two arguments: - elements: a slice of elements of type E to be filtered. - filter: a function that defines the condition for including elements. - It returns a new slice containing the filtered elements.
func Map ¶
func Map[E, V any](elements []E, mapper MapperFunc[E, V]) []V
Map applies a mapping function to each element in a slice and returns a new slice of transformed elements. It takes three arguments: - elements: a slice of elements of type E to be transformed. - mapper: a function that defines how each element will be transformed. - It returns a new slice of type V containing the transformed elements.
func NextPermute ¶
NextPermute find the next permuted if possible Returns ok, elements. If next permutation is exhausted ok is false The algorithm goes like: - Find the largest index of k such that a[k] < a[k+1] - Find the largest index l such that a[l] > a[k] - Swap the value of a[l] and a[k], and - Reverse the list
func Permutations ¶
Permutations gets all the possible permutations This version doesn't gurrantee lexical ordered
func PermutationsOrdered ¶
PermutationsOrdered same as Permutations but lexically ordered
func Reduce ¶
func Reduce[E, V any](elements []E, reducer ReducerFunc[E, V], accumulator V) V
Reduce applies a reducer function to each element in a slice and accumulates the results. It takes three arguments:
- elements: a slice of elements of type E that you want to reduce.
- reducer: a function that defines how the reduction will proceed. It takes the current accumulator value (V), the next element (E), and the index (int) of the element being processed.
- accumulator: the initial value of the accumulator, of type V, which is also the final result type after the reduction.
The function returns the final accumulator value after processing all elements.
func Some ¶
func Some[E any](elements []E, predicate PredicateFunc[E]) bool
Some checks if at least one element in the slice meets the condition defined by the provided predicate function. It takes two arguments: - elements: a slice of elements of type E to be checked. - predicate: a function that defines the condition to check for each element. It returns true if at least one element satisfies the condition; otherwise, false.
Types ¶
type FilterFunc ¶
FilterFunc is a generic function type that defines the condition for including an element in the filtered result. It takes two parameters: - element: the current element of type E being processed. - index: the index of the current element in the collection. It returns true if the element should be included in the filtered result.
type MapperFunc ¶
MapperFunc is a generic function type that defines how each element in a collection is transformed. It takes two parameters: - element: the current element of type E being processed. - index: the index of the current element in the collection. It returns the transformed element of type V.
type PredicateFunc ¶
PredicateFunc is a generic function type that defines the condition for determining if at least one or all elements in a collection meets the criteria. It takes two parameters: - element: the current element of type E being processed. - index: the index of the current element in the collection. It returns true if the element meets the criteria.
type ReducerFunc ¶
ReducerFunc is a generic function type that defines the operation to be applied to each element in a collection during reduction. It takes three arguments: - accumulator: the cumulative result (of type V) that is carried forward. - next: the next element (of type E) in the collection to be processed. - index: the index of the current element in the collection. It returns the updated accumulator (of type V).