Documentation
¶
Index ¶
- Variables
- func All[T any](items []T, filters ...filtering.Expression[T]) bool
- func AllWithContext[T any](ctx context.Context, items []T, filters ...filtering.ExpressionWithContext[T]) (bool, error)
- func Any[T any](items []T, filters ...filtering.Expression[T]) bool
- func AnyWithContext[T any](ctx context.Context, items []T, filters ...filtering.ExpressionWithContext[T]) (bool, error)
- func Chunk[T any](items []T, n int) [][]T
- func ChunkMap[K comparable, V any](items map[K]V, n int) []map[K]V
- func Coalesce[T comparable](items ...T) T
- func Combinations[T any](items []T, size int) [][]T
- func CombinationsFiltered[T any](items []T, size int, filter filtering.Expression[T]) [][]IndexedItem[T]
- func Compact[T any, K comparable](input []T, keyMapper SliceMapperExpression[T, K]) []T
- func Concatenate[T any](lists ...[]T) []T
- func Contains[T comparable](items []T, value T) bool
- func Count[T any](items []T, filters ...filtering.Expression[T]) int
- func CountWithContext[T any](ctx context.Context, items []T, filters ...filtering.ExpressionWithContext[T]) (int, error)
- func Cut[T any](items []T) (T, []T)
- func DefaultIfEmpty[T any](items []T, def T) []T
- func Distinct[S ~[]E, E cmp.Ordered](in S) S
- func DistinctFunc[S ~[]E, E any](x S, cmp func(a, b E) int) S
- func DistinctStable[S ~[]E, E comparable](in S) S
- func DistinctStableFunc[S ~[]E, E any](in S, hash func(val E) uint64) S
- func Except[T comparable](items []T, exclusions ...T) []T
- func Filter[T any](items []T, filters ...filtering.Expression[T]) []T
- func FilterWithContext[T any](ctx context.Context, items []T, filters ...filtering.ExpressionWithContext[T]) ([]T, error)
- func First[T any](items []T, filters ...filtering.Expression[T]) T
- func FirstIndexOf[T comparable](v T, items []T) int
- func FirstWithContext[T any](ctx context.Context, items []T, filters ...filtering.ExpressionWithContext[T]) (T, error)
- func Group[T any, K comparable](items []T, keyMapper SliceMapperExpression[T, K]) map[K][]T
- func GroupWithContext[T any, K comparable](ctx context.Context, items []T, ...) (map[K][]T, error)
- func If[T any](condition bool, ifTrue, ifFalse T) T
- func Intersect[T comparable](items []T, others ...T) []T
- func KeyValuesToMap[K comparable, V any](input []KeyValuePair[K, V]) map[K]V
- func Keys[K comparable, V any](input map[K]V) []K
- func Last[T any](items []T, filters ...filtering.Expression[T]) T
- func LastIndexOf[T comparable](v T, items []T) int
- func LastWithContext[T any](ctx context.Context, items []T, filters ...filtering.ExpressionWithContext[T]) (T, error)
- func Map[T any, V any](items []T, mapper SliceMapperExpression[T, V]) []V
- func MapValues[K comparable, V any, NV any](input map[K]V, mapper func(k K, v V) NV) map[K]NV
- func MapValuesWithContext[K comparable, V any, NV any](ctx context.Context, input map[K]V, ...) (map[K]NV, error)
- func MapWithContext[T any, V any](ctx context.Context, items []T, mapper SliceMapperExpressionWithContext[T, V]) ([]V, error)
- func Max[T Numeric](items []T) T
- func MaxConcurrent[I any, O any](n int, execute func(ctx context.Context, in I) (O, error)) func(context.Context, I) (O, error)
- func Min[T Numeric](items []T) T
- func Must[T any](v T, err error) T
- func PointerOrNil[T comparable](v T) *T
- func PointerTo[T any](v T) *T
- func Repeat[T any](v T, count int) []T
- func Reverse[T any](items []T) []T
- func Skip[T any](items []T, n int) []T
- func Sort[T Comparable](in []T) []T
- func SortDescending[T Comparable](in []T) []T
- func Sum[T Numeric](items []T) T
- func Take[T any](items []T, n int) []T
- func TakeUntil[T any](items []T, filters ...filtering.Expression[T]) []T
- func TakeUntilWithContext[T any](ctx context.Context, items []T, filters ...filtering.ExpressionWithContext[T]) ([]T, error)
- func TakeWhile[T any](items []T, filters ...filtering.Expression[T]) []T
- func TakeWhileWithContext[T any](ctx context.Context, items []T, filters ...filtering.ExpressionWithContext[T]) ([]T, error)
- func ToMap[T any, K comparable, V any](items []T, keyMapper SliceMapperExpression[T, K], ...) map[K]V
- func ToMapWithContext[T any, K comparable, V any](ctx context.Context, items []T, ...) (map[K]V, error)
- func ValueOrDefault[T any](v *T) T
- func ValueOrError[T any](value T, err error) (T, error)
- func ValueOrFailTest[T any](t *testing.T, v T, err error) T
- func ValueOrPanic[T any](v T, err error) T
- func Values[K comparable, V any](input map[K]V) []V
- type CachedTask
- type Comparable
- type IndexedItem
- type KeyValuePair
- type Numeric
- type Resolver
- type ResolverWorkFn
- type SliceMapperExpression
- type SliceMapperExpressionWithContext
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrEmptySlice = errors.New("operator cannot be applied to empty slice")
ErrEmptySlice indicates an error applying an operator to an empty slice without a suitable default or other fallback.
var ErrTupleHasError = errors.New("the tail member of the input tuple contained an error")
ErrTupleHasError is our
Functions ¶
func All ¶
func All[T any](items []T, filters ...filtering.Expression[T]) bool
All returns true if no item in the list fails to meet the predicates. If multiple predicates are passed, they are treated as logical AND operations.
func AllWithContext ¶
func AllWithContext[T any](ctx context.Context, items []T, filters ...filtering.ExpressionWithContext[T]) (bool, error)
AllWithContext is a context aware function that returns true if no item in the list fails to meet the predicate. If multiple predicate passed, they are treated as logical AND operations.
func Any ¶
func Any[T any](items []T, filters ...filtering.Expression[T]) bool
Any returns true if any item in the slice matches the filter. If multiple predicates are passed, they are treated as logical AND operations.
func AnyWithContext ¶
func AnyWithContext[T any](ctx context.Context, items []T, filters ...filtering.ExpressionWithContext[T]) (bool, error)
AnyWithContext is a context aware function that returns true if any item in the slice matches the filter.
func Chunk ¶
Chunk will uniformly distribute the given over n bins/buckets. Note that buckets are slices referencing the same memory.
func ChunkMap ¶
func ChunkMap[K comparable, V any](items map[K]V, n int) []map[K]V
ChunkMap will uniformly distribute the given map over n bins/buckets.
func Coalesce ¶
func Coalesce[T comparable](items ...T) T
Coalesce returns the first value in the input that is not the default value forthe type.
func Combinations ¶
Combinations generates all combinations of the input objects at the specified size.
func CombinationsFiltered ¶
func CombinationsFiltered[T any](items []T, size int, filter filtering.Expression[T]) [][]IndexedItem[T]
CombinationsFiltered returns the combinations of items, but applies a filter to the items. The returned indexed items represent the original positions in the raw list.
func Compact ¶
func Compact[T any, K comparable](input []T, keyMapper SliceMapperExpression[T, K]) []T
Compact takes a slice and compacts it, by reducing to only the last occurrence of a given key. This is akin to Kafka topic compaction, and used for scenarios where you have a slice of mixed updates but want to take only the final update for a given predicate. The result order is determined by the final position(s) of the surviving elements relative to each other.
func Contains ¶
func Contains[T comparable](items []T, value T) bool
Contains returns true if the set contains the specified value
func Count ¶
func Count[T any](items []T, filters ...filtering.Expression[T]) int
Count returns how many items pass the filters
func CountWithContext ¶
func CountWithContext[T any](ctx context.Context, items []T, filters ...filtering.ExpressionWithContext[T]) (int, error)
CountWithContext counts how many items pass the filter
func Cut ¶
func Cut[T any](items []T) (T, []T)
Cut removes the head of a list, returning it and the remainder of the list. If the input list is empty, cut returns the type-default.
func DefaultIfEmpty ¶
func DefaultIfEmpty[T any](items []T, def T) []T
DefaultIfEmpty checks to see if the specified slice is empty and if so, creates a slice with a specified default value.
func Distinct ¶
Distinct sorts and removes any duplicate elements from a slice, returning a new copy. The input slice is unchanged.
func DistinctFunc ¶
DistinctFunc is like Distinct but uses a custom comparison function on each pair of elements.
DistinctFunc requires that cmp is a strict weak ordering. See https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings.
func DistinctStable ¶
func DistinctStable[S ~[]E, E comparable](in S) S
DistinctStable removes any duplicates from a slice, keeping iteration order and returning a new copy. The input slice is unchanged.
func DistinctStableFunc ¶
DistinctStableFunc is like DistinctStable but uses a custom hash function to deduplicate the elements.
Elements with the same hash are considered to be equal, collisions are NOT considered.
func Filter ¶
func Filter[T any](items []T, filters ...filtering.Expression[T]) []T
Filter filters item in a list
func FilterWithContext ¶
func FilterWithContext[T any](ctx context.Context, items []T, filters ...filtering.ExpressionWithContext[T]) ([]T, error)
FilterWithContext filters item in a list
func First ¶
func First[T any](items []T, filters ...filtering.Expression[T]) T
First item in a slice that passes the filters. If multiple filters are set, they are treated as a logical AND. If no filters are set, will return the first item in the slice. If no items match, the type default is returned.
func FirstIndexOf ¶
func FirstIndexOf[T comparable](v T, items []T) int
FirstIndexOf returns the first index of an item in the slice
func FirstWithContext ¶
func FirstWithContext[T any](ctx context.Context, items []T, filters ...filtering.ExpressionWithContext[T]) (T, error)
FirstWithContext gets the first item in a slice that passes the filters. If multiple filters are set, they are treated as a logical AND. If no filters are set, will return the first item in the slice. If no items match, the type default is returned.
func Group ¶
func Group[T any, K comparable](items []T, keyMapper SliceMapperExpression[T, K]) map[K][]T
Group rolls up items into groups based on a mapper function that provides a key per item
func GroupWithContext ¶
func GroupWithContext[T any, K comparable](ctx context.Context, items []T, keyMapper SliceMapperExpressionWithContext[T, K]) (map[K][]T, error)
GroupWithContext rolls up items into groups based on a mapper function that provides a key per item
func KeyValuesToMap ¶
func KeyValuesToMap[K comparable, V any](input []KeyValuePair[K, V]) map[K]V
KeyValuesToMap converts a slice of key-value pairs to a map
func Last ¶
func Last[T any](items []T, filters ...filtering.Expression[T]) T
Last item in a slice that matches the specified filters. Returns the type default if none found.
func LastIndexOf ¶
func LastIndexOf[T comparable](v T, items []T) int
LastIndexOf returns the last index of an item in the slice
func LastWithContext ¶
func LastWithContext[T any](ctx context.Context, items []T, filters ...filtering.ExpressionWithContext[T]) (T, error)
LastWithContext item in a slice that matches the specified filters. Returns the type default if none found.
func Map ¶
func Map[T any, V any](items []T, mapper SliceMapperExpression[T, V]) []V
Map converts values in a slice from one type to another
func MapValues ¶
func MapValues[K comparable, V any, NV any](input map[K]V, mapper func(k K, v V) NV) map[K]NV
MapValues translates all values in a map to new values
func MapValuesWithContext ¶
func MapValuesWithContext[K comparable, V any, NV any](ctx context.Context, input map[K]V, mapper func(ctx context.Context, k K, v V) (NV, error)) (map[K]NV, error)
MapValuesWithContext translates all values in a map to new values
func MapWithContext ¶
func MapWithContext[T any, V any](ctx context.Context, items []T, mapper SliceMapperExpressionWithContext[T, V]) ([]V, error)
MapWithContext executes a mapper over the members of a slice using the specified context
func Max ¶
func Max[T Numeric](items []T) T
Max gets the maximum of numeric values. If the slice is empty then a value of 0 is returned.
func MaxConcurrent ¶
func MaxConcurrent[I any, O any](n int, execute func(ctx context.Context, in I) (O, error)) func(context.Context, I) (O, error)
MaxConcurrent limits the maximum number of executions in flight for a function by using a pool of tokens. Each time a request starts executing, a token is obtained - then subsequently released. The function is panic safe, and will release tokens cleanly through the use of a defer.
func Min ¶
func Min[T Numeric](items []T) T
Min gets the minimum of numeric values. If the slice is empty then a value of 0 is returned.
func Must ¶
Must enforces that a value/error pair contains no error, and returns the value. If an error is present, the code will panic. If you require a default value instead use MustDefault instead.
Example ¶
package main import ( "errors" "fmt" "github.com/zeroflucs-given/generics" ) func main() { // Any function that returns a value/error tuple fn := func(fail bool) (string, error) { if fail { return "", errors.New("boom") } return "Hello", nil } // Instead of assigning to two variables, can step straight to value v := generics.Must(fn(false)) fmt.Println(v) }
Output:
func PointerOrNil ¶
func PointerOrNil[T comparable](v T) *T
PointerOrNil returns a nil if the value is its type default, or a pointer to the value if it's set.
func Sort ¶
func Sort[T Comparable](in []T) []T
Sort creates a sorted version of the list, whilst leaving the original list intact.
func SortDescending ¶
func SortDescending[T Comparable](in []T) []T
SortDescending creates a reverse sorted version of the list, whilst leaving the original list intact.
func TakeUntil ¶
func TakeUntil[T any](items []T, filters ...filtering.Expression[T]) []T
TakeUntil takes items from the slice until the first item that passes the predicate.
func TakeUntilWithContext ¶
func TakeUntilWithContext[T any](ctx context.Context, items []T, filters ...filtering.ExpressionWithContext[T]) ([]T, error)
TakeUntilWithContext takes items from the slice until the first item that passes the predicate.
func TakeWhile ¶
func TakeWhile[T any](items []T, filters ...filtering.Expression[T]) []T
TakeWhile takes items from the slice until the first item that fails the predicate.
func TakeWhileWithContext ¶
func TakeWhileWithContext[T any](ctx context.Context, items []T, filters ...filtering.ExpressionWithContext[T]) ([]T, error)
TakeWhileWithContext takes items from the slice until the first item that fails the predicate.
func ToMap ¶
func ToMap[T any, K comparable, V any](items []T, keyMapper SliceMapperExpression[T, K], valueMapper SliceMapperExpression[T, V]) map[K]V
ToMap converts a slice of items into a dictionary using mappers for the key and value pairs. If multiple items yield the same key, the last key in the set will be the one kept.
func ToMapWithContext ¶
func ToMapWithContext[T any, K comparable, V any](ctx context.Context, items []T, keyMapper SliceMapperExpressionWithContext[T, K], valueMapper SliceMapperExpressionWithContext[T, V]) (map[K]V, error)
ToMapWithContext converts a slice of items into a dictionary using mappers for the key and value pairs. If multiple items yield the same key, the last key in the set will be the one kept. If any mapper fails, the operation as a whole fails.
func ValueOrDefault ¶
func ValueOrDefault[T any](v *T) T
ValueOrDefault safely resolves a pointer to a type
func ValueOrError ¶
ValueOrError returns an error only if the error is set, otherwise returns the value and nil. This replaces the value with the default/nil for its type.
func ValueOrFailTest ¶
ValueOrFailTest gets a value, or fails a test if an error is present. This simplifies some test code.
func ValueOrPanic ¶
ValueOrPanic consumes an error and panics, simplifying some code. We recommend only using this in the context of tools.
func Values ¶
func Values[K comparable, V any](input map[K]V) []V
Values gets the set of keys in a map
Types ¶
type CachedTask ¶
type CachedTask[T any] struct { // contains filtered or unexported fields }
CachedTask holds a packet of work that is executed once, and the result retrieved many times.
func ExecuteOnce ¶
func ExecuteOnce[T any](fn func(ctx context.Context) (*T, error)) CachedTask[T]
ExecuteOnce creates a new CachedTask for which the work is provided by fn.
type Comparable ¶
Comparable is the set of comparable types for our operators
type IndexedItem ¶
IndexedItem is an item that is indexed
type KeyValuePair ¶
type KeyValuePair[K comparable, V any] struct { Key K Value V }
KeyValuePair is a pairing of key/values, for when we have to represent map sets as a list/slice.
func SortedByKey ¶
func SortedByKey[K Comparable, V any](input map[K]V) []KeyValuePair[K, V]
SortedByKey sorts a map by key and returns it as a slice of key-value pairs enabling working with the map in a reliable/repeatable order
func ToKeyValues ¶
func ToKeyValues[K comparable, V any](input map[K]V) []KeyValuePair[K, V]
ToKeyValues converts a map into a set of key/value pairs
type Numeric ¶
type Numeric interface { ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64 }
Numeric is a type that contains all the available numeric types known to go
type Resolver ¶
type Resolver[K comparable, V any] struct { WorkFn ResolverWorkFn[K, V] // Work function // contains filtered or unexported fields }
Resolver is a type that lets us do hard work once, and share the results across multiple callers. Also handles parallelism.
type ResolverWorkFn ¶
type ResolverWorkFn[K comparable, V any] func(ctx context.Context, key K) (V, error)
ResolverWorkFn is a function that resolvers use to do the underlying work
type SliceMapperExpression ¶
SliceMapperExpression is a type that represents a generic mapper