Documentation
¶
Index ¶
- func All[T any](s []T, fn func(T) bool) bool
- func Any[T any](s []T, fn func(T) bool) bool
- func AppendToGroup[M ~map[K][]V, K comparable, V any](m M, k K, v V)
- func Associate[T, V any, K comparable](s []T, fn func(T) (K, V)) map[K]V
- func Chunked[T any](s []T, chunkSize int) [][]T
- func ChunkedBy[T any](s []T, fn func(T, T) bool) [][]T
- func Distinct[T comparable](s []T) []T
- func DistinctBy[T any, K comparable](s []T, fn func(T) K) []T
- func Drop[T any](s []T, n int) []T
- func DropLast[T any](s []T, n int) []T
- func DropLastWhile[T any](s []T, fn func(T) bool) []T
- func DropWhile[T any](s []T, fn func(T) bool) []T
- func Filter[T any](s []T, fn func(T) bool) []T
- func FilterIndexed[T any](s []T, fn func(int, T) bool) []T
- func FilterMap[T1, T2 any](s []T1, fn func(T1) (T2, bool)) []T2
- func FlatMap[T1, T2 any](s []T1, fn func(T1) []T2) []T2
- func FlatMapIndexed[T1, T2 any](s []T1, fn func(int, T1) []T2) []T2
- func Fold[T, R any](s []T, initial R, fn func(R, T) R) R
- func FoldIndexed[T, R any](s []T, initial R, fn func(int, R, T) R) R
- func FoldItems[M ~map[K]V, K comparable, V, R any](m M, initial R, fn func(R, K, V) R) R
- func GetOrInsert[M ~map[K]V, K comparable, V any](m M, k K, fn func(K) V) V
- func GroupBy[T, V any, K comparable](s []T, fn func(T) (K, V)) map[K][]V
- func Map[T1, T2 any](s []T1, fn func(T1) T2) []T2
- func MapIndexed[T1, T2 any](s []T1, fn func(int, T1) T2) []T2
- func Partition[T any](s []T, fn func(T) bool) ([]T, []T)
- func Reduce[T any](s []T, fn func(T, T) T) T
- func ReduceIndexed[T any](s []T, fn func(int, T, T) T) T
- func Reverse[T any](s []T)
- func Reversed[T any](s []T) []T
- func Take[T any](s []T, n int) []T
- func TakeLast[T any](s []T, n int) []T
- func TakeLastWhile[T any](s []T, fn func(T) bool) []T
- func TakeWhile[T any](s []T, fn func(T) bool) []T
- func TransformMap[M ~map[K]V, K comparable, V any](m M, fn func(k K, v V) (K, V, bool)) M
- func Unzip[T1 any, T2 any](ps []*Pair[T1, T2]) ([]T1, []T2)
- func Windowed[T any](s []T, size, step int) [][]T
- type Pair
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendToGroup ¶
func AppendToGroup[M ~map[K][]V, K comparable, V any](m M, k K, v V)
AppendToGroup adds the key, value to the given map where each key points to a slice of values
func Associate ¶
func Associate[T, V any, K comparable](s []T, fn func(T) (K, V)) map[K]V
Associate returns a map containing key-value pairs returned by the given function applied to the elements of the given slice
func Chunked ¶
Chunked splits the slice into a slice of slices, each not exceeding given size The last slice might have fewer elements than the given size
func Distinct ¶
func Distinct[T comparable](s []T) []T
Distinct returns a slice containing only distinct elements from the given slice Elements will retain their original order.
func DistinctBy ¶
func DistinctBy[T any, K comparable](s []T, fn func(T) K) []T
DistinctBy returns a slice containing only distinct elements from the given slice as distinguished by the given selector function Elements will retain their original order.
func DropLastWhile ¶
DropLastWhile returns a slice containing all elements except the last elements that satisfy the given predicate
func DropWhile ¶
DropWhile returns a slice containing all elements except the first elements that satisfy the given predicate
func Filter ¶
Filter returns the slice obtained after retaining only those elements in the given slice for which the given function returns true
func FilterIndexed ¶
FilterIndexed returns the slice obtained after retaining only those elements in the given slice for which the given function returns true. Predicate receives the value as well as its index in the slice.
func FilterMap ¶
FilterMap returns the slice obtained after both filtering and mapping using the given function. The function should return two values - first, the result of the mapping operation and second, whether the element should be included or not. This is faster than doing a separate filter and map operations, since it avoids extra allocations and slice traversals.
func FlatMap ¶
func FlatMap[T1, T2 any](s []T1, fn func(T1) []T2) []T2
FlatMap transforms a slice of T1 elementss (s) into a slice of T2 elements. The transformation is defined by the function fn, which takes a T1 element and returns a slice of T2 elements. This function applies fn to every element in s, and combines the results into a single, "flattened" slice of T2 elements.
func FlatMapIndexed ¶
FlatMapIndexed transforms a slice of T1 elements (s) into a slice of T2 elements. The transformation is defined by the function fn, which takes a T1 element and the index to the element, and returns a slice of T2 elements. This function applies fn to every element in s, and combines the results into a single, "flattened" slice of T2 elements.
func Fold ¶
func Fold[T, R any](s []T, initial R, fn func(R, T) R) R
Fold accumulates values starting with given initial value and applying given function to current accumulator and each element.
func FoldIndexed ¶
FoldIndexed accumulates values starting with given initial value and applying given function to current accumulator and each element. Function also receives index of current element.
func FoldItems ¶
func FoldItems[M ~map[K]V, K comparable, V, R any]( m M, initial R, fn func(R, K, V) R, ) R
FoldItems accumulates values starting with given intial value and applying given function to current accumulator and each key, value.
func GetOrInsert ¶
func GetOrInsert[M ~map[K]V, K comparable, V any](m M, k K, fn func(K) V) V
GetOrInsert checks if a value corresponding to the given key is present in the map. If present it returns the existing value. If not, it invokes the given callback function to get a new value for the given key, inserts it in the map and returns the new value
func GroupBy ¶
func GroupBy[T, V any, K comparable]( s []T, fn func(T) (K, V), ) map[K][]V
GroupBy returns a map containing key to list of values returned by the given function applied to the elements of the given slice
func Map ¶
func Map[T1, T2 any](s []T1, fn func(T1) T2) []T2
Map returns the slice obtained after applying the given function over every element in the given slice
func MapIndexed ¶
MapIndexed returns the slice obtained after applying the given function over every element in the given slice. The function also receives the index of each element in the slice.
func Partition ¶
Partition returns two slices where the first slice contains elements for which the predicate returned true and the second slice contains elements for which it returned false.
func Reduce ¶
func Reduce[T any](s []T, fn func(T, T) T) T
Reduce accumulates the values starting with the first element and applying the operation from left to right to the current accumulator value and each element The input slice must have at least one element.
func ReduceIndexed ¶
ReduceIndexed accumulates the values starting with the first element and applying the operation from left to right to the current accumulator value and each element The input slice must have at least one element. The function also receives the index of the element.
func Reversed ¶
func Reversed[T any](s []T) []T
Reversed returns a new list with the elements in reverse order
func Take ¶
Take returns the slice obtained after taking the first n elements from the given slice. If n is greater than the length of the slice, returns the entire slice
func TakeLast ¶
TakeLast returns the slice obtained after taking the last n elements from the given slice.
func TakeLastWhile ¶
TakeLastWhile returns a slice containing the last elements satisfying the given predicate
func TakeWhile ¶
TakeWhile returns a list containing the first elements satisfying the given predicate
func TransformMap ¶
func TransformMap[M ~map[K]V, K comparable, V any]( m M, fn func(k K, v V) (K, V, bool), ) M
TransformMap applies the given function to each key, value in the map, and returns a new map of the same type after transforming the keys and values depending on the callback functions return values. If the last bool return value from the callback function is false, the entry is dropped
Types ¶
type Pair ¶
type Pair[T1, T2 any] struct { Fst T1 Snd T2 }
Pair represents a generic pair of two values
func Items ¶
func Items[M ~map[K]V, K comparable, V any](m M) []*Pair[K, V]
Items returns the (key, value) pairs of the given map as a slice