Documentation ¶
Overview ¶
Package gog provides utilities for working with Go generics.
Index ¶
- func Dedupe[T comparable](slice []T) []T
- func FindFunc[T any](slice []T, fn func(e T) bool) (T, bool)
- func FindFuncPtr[T any](slice []T, fn func(e T) bool) *T
- func Flatten[T any](slice [][]T) []T
- func Map[S, T any](slice []S, fn func(S) T) []T
- func Mapping[S any, K comparable, V any](slice []S, fn func(S) (K, V)) map[K][]V
- func Mutate[T any](slice []T, fn func(e *T))
- func Partition[S any, K comparable](slice []S, fn func(S) K) map[K][]S
- func PtrOf[T any](v T) *T
- func Reduce[S, T any](slice []S, initialValue T, fn func(accum T, value S) T) T
- func Select[S any](slice []S, fn func(S) bool) []S
- func ValueOf[T any](v *T, defaultValue T) T
- type KV
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dedupe ¶ added in v0.2.0
func Dedupe[T comparable](slice []T) []T
Dedupe returns a new slice that contains only distinct elements from the original slice.
func FindFunc ¶ added in v0.7.0
FindFunc iterates over the slice and uses the provided closure function to check whether the elements match a user-provided condition. The first value that matches is returned as well as a true flag. Otherwise a false flag is returned.
func FindFuncPtr ¶ added in v0.7.0
FindFuncPtr iterates over the slice and uses the provided closure function to check whether the elements match a user-provided condition. A pointer to the first element that passes the condition is returned. If no value is applicable, then nil is returned.
func Flatten ¶ added in v0.3.0
func Flatten[T any](slice [][]T) []T
Flatten returns a new slice that is the result of merging all nested slices into a single top-level slice.
func Map ¶
func Map[S, T any](slice []S, fn func(S) T) []T
Map can be used to transform one slice into another by providing a function to do the mapping.
func Mapping ¶ added in v0.3.0
func Mapping[S any, K comparable, V any](slice []S, fn func(S) (K, V)) map[K][]V
Mapping is similar to Partition, except that it allows one to transform the values stored in the partition buckets. In essence, it allows the caller to construct an almost arbitrary map (it is always of kind map[key][]value, though the types of the keys and the values are user controlled) from an arbitrary slice.
func Mutate ¶ added in v0.7.0
func Mutate[T any](slice []T, fn func(e *T))
Mutate iterates over the slice and calls the provided closure function, passing a pointer to each element. This allows the user to easily modify all the elements of a slice.
func Partition ¶
func Partition[S any, K comparable](slice []S, fn func(S) K) map[K][]S
Partition splits the specified slice into groups, each having a key according to the specified function.
func Reduce ¶
func Reduce[S, T any](slice []S, initialValue T, fn func(accum T, value S) T) T
Reduce compacts a slice into a single value. The provided function is used to perform the reduction starting with the initialValue.
Types ¶
type KV ¶ added in v0.3.0
type KV[K comparable, V any] struct { // Key holds the key of this key-value pair. Key K // Value holds the value of this key-value pair. Value V }
KV represents a key-value pair.
func Entries ¶ added in v0.3.0
func Entries[K comparable, V any](m map[K]V) []KV[K, V]
Entries returns a slice of key-value pair mappings that make up the specified map.
Note: There is no guarantee as to the order of the returned entries, nor is that order guaranteed to be consistent across two calls.
Directories ¶
Path | Synopsis |
---|---|
Package ds provides various data structure implementations with generics.
|
Package ds provides various data structure implementations with generics. |
Package filter adds helper functions for performing filtering over a data set.
|
Package filter adds helper functions for performing filtering over a data set. |
Package opt provides utilities for optional types.
|
Package opt provides utilities for optional types. |