collection

package
v1.161.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

  • Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved.
  • SPDX-License-Identifier: Apache-2.0

Package collection provides various utilities working on slices or sequences

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(slice []bool) bool

All returns true if all items of the slice are true.

func AllFunc added in v1.78.0

func AllFunc[S ~[]E, E any](s S, f Predicate[E]) bool

AllFunc returns true if f returns true for every element in s.

func AllNotEmpty added in v1.49.0

func AllNotEmpty(strict bool, slice []string) bool

AllNotEmpty returns true when every entry of slice is non-empty. If strict is true, strings containing only whitespace are considered empty.

func AllSequence added in v1.134.0

func AllSequence(seq iter.Seq[bool]) bool

AllSequence returns true if all items of the sequence are true.

func AllTrue added in v1.78.0

func AllTrue(values ...bool) bool

AllTrue returns whether all values are true.

func AllTrueSequence added in v1.134.0

func AllTrueSequence[E any](s iter.Seq[E], f Predicate[E]) bool

AllTrueSequence returns true if f returns true for every element in the sequence.

func And added in v1.80.0

func And(values ...bool) bool

And performs an 'and' operation on an array of booleans.

func Any

func Any(slice []bool) bool

Any returns true if there is at least one element of the slice which is true.

func AnyEmpty added in v1.49.0

func AnyEmpty(strict bool, slice []string) bool

AnyEmpty returns true if any entry of slice is empty. If strict is true, strings containing only whitespace are considered empty.

func AnyFalse added in v1.134.0

func AnyFalse(values ...bool) bool

AnyFalse returns whether there is a value set to false

func AnyFalseSequence added in v1.134.0

func AnyFalseSequence(eq iter.Seq[bool]) bool

AnyFalseSequence returns true if there is at least one element of the sequence which is false. If the sequence is empty, it also returns true.

func AnyFunc added in v1.78.0

func AnyFunc[S ~[]E, E any](s S, f Predicate[E]) bool

AnyFunc returns true if at least one element in s satisfies f.

func AnyRefFunc added in v1.147.0

func AnyRefFunc[S ~[]E, E any](s S, f PredicateRef[E]) bool

AnyRefFunc behaves like AnyFunc but accepts a reference-based predicate.

func AnySequence added in v1.134.0

func AnySequence(seq iter.Seq[bool]) bool

AnySequence returns true if there is at least one element of the slice which is true.

func AnyTrue added in v1.78.0

func AnyTrue(values ...bool) bool

AnyTrue returns whether there is a value set to true

func ConvertListOfPairsToMap added in v1.97.0

func ConvertListOfPairsToMap(input []string, pairSeparator string) (pairs map[string]string, err error)

func ConvertListOfPairsToMapWithOptions added in v1.150.0

func ConvertListOfPairsToMapWithOptions(input []string, opts ...PairOption) (pairs map[string]string, err error)

func ConvertMapToCommaSeparatedList added in v1.96.0

func ConvertMapToCommaSeparatedList[K comparable, V any](pairs map[K]V) string

ConvertMapToCommaSeparatedList converts a map to a string of comma separated list of keys and values defined sequentially

func ConvertMapToCommaSeparatedPairsList added in v1.96.0

func ConvertMapToCommaSeparatedPairsList[K comparable, V any](pairs map[K]V, pairSeparator string) string

ConvertMapToCommaSeparatedPairsList converts a map to a string of comma separated list of key, value pairs.

func ConvertMapToLoggerValues added in v1.116.0

func ConvertMapToLoggerValues[K comparable, V any](pairs map[K]V) []any

ConvertMapToLoggerValues is similar to ConvertMapToSlice but returns a type compatible with logr.Logger.

func ConvertMapToOrderedCommaSeparatedList added in v1.128.0

func ConvertMapToOrderedCommaSeparatedList[K cmp.Ordered, V any](pairs map[K]V) string

ConvertMapToOrderedCommaSeparatedList converts a map to a commar-separated list and ensures that the ordering will always be the same

func ConvertMapToOrderedSlice added in v1.128.0

func ConvertMapToOrderedSlice[K cmp.Ordered, V any](pairs map[K]V) []string

ConvertMapToOrderedSlice converts a map to a slice and ensures that the ordering will always be the same

func ConvertMapToPairSlice added in v1.96.0

func ConvertMapToPairSlice[K comparable, V any](pairs map[K]V, pairSeparator string) []string

ConvertMapToPairSlice converts a map to list of key value pairs e.g. ["key1=value1", "key2=value2"]

func ConvertMapToSlice added in v1.96.0

func ConvertMapToSlice[K comparable, V any](pairs map[K]V) []string

ConvertMapToSlice converts a map to list of keys and values listed sequentially e.g. [key1, value1, key2, value2]

func ConvertSliceToCommaSeparatedList added in v1.96.0

func ConvertSliceToCommaSeparatedList[T any](slice []T) string

ConvertSliceToCommaSeparatedList converts a slice into a string containing a coma separated list

func ConvertSliceToMap added in v1.97.0

func ConvertSliceToMap[T comparable](input []T) (pairs map[T]T, err error)

ConvertSliceToMap converts a slice of elements into a map e.g. [key1, value1, key2, values2] -> {key1: value1, key2: value2}

func CountBy added in v1.161.0

func CountBy[S ~[]E, E any](slice S, predicate Predicate[E]) int

CountBy counts how many elements in a slice satisfy the predicate.

This is useful when a collection should be reduced to the number of matching elements, for example, counting even values, enabled items, or values that match a threshold.

Reference documentation:

func CountByRef added in v1.161.0

func CountByRef[S ~[]E, E any](slice S, predicate PredicateRef[E]) int

CountByRef behaves like CountBy but accepts a reference-based predicate.

func CountByRefSequence added in v1.161.0

func CountByRefSequence[E any](sequence iter.Seq[E], predicate PredicateRef[E]) int

CountByRefSequence behaves like CountBySequence but accepts a reference-based predicate.

func CountBySequence added in v1.161.0

func CountBySequence[E any](sequence iter.Seq[E], predicate Predicate[E]) int

CountBySequence counts how many elements in a sequence satisfy predicate.

This is useful when values are processed lazily through iterators rather than from a prebuilt slice.

func Difference added in v1.134.0

func Difference[T comparable](slice1, slice2 []T) []T

Difference returns distinct values present in slice1 but not in slice2.

func Each added in v1.134.0

func Each[T any](s iter.Seq[T], f OperationFunc[T]) error

Each iterates over a sequence and invokes f for each element. If f returns a non-EOF error, iteration stops and that error is returned. If f returns EOF, the EOF is ignored and iteration ends without error.

func EachRef added in v1.147.0

func EachRef[T any](s iter.Seq[T], f OperationRefFunc[T]) error

EachRef behaves like Each but invokes f with a reference to each element.

func Filter added in v1.110.0

func Filter[S ~[]E, E any](s S, f FilterFunc[E]) S

Filter returns a new slice containing elements from s for which f returns true.

This is useful when a collection should be narrowed down to only the items of interest, for example active users, matching paths, or values that pass a validation predicate.

Reference documentation:

func FilterRef added in v1.147.0

func FilterRef[S ~[]E, E any](s S, f FilterRefFunc[E]) S

FilterRef behaves like Filter but accepts a reference-based predicate.

This is useful when the predicate naturally works on pointers or when it is shared with other APIs that already use PredicateRef.

func FilterRefSequence added in v1.147.0

func FilterRefSequence[E any](s iter.Seq[E], f PredicateRef[E]) (result iter.Seq[E])

FilterRefSequence behaves like FilterSequence but accepts a reference-based predicate.

This is useful when sequence processing should remain lazy while the predicate logic is written against PredicateRef.

func FilterSequence added in v1.134.0

func FilterSequence[E any](s iter.Seq[E], f Predicate[E]) (result iter.Seq[E])

FilterSequence returns a sequence that yields only elements for which f returns true.

This is the lazy sequence-oriented counterpart to Filter and is useful when composing iterator pipelines, avoiding intermediate slices, or processing potentially large or streaming inputs incrementally.

Reference documentation:

func Find

func Find(slice *[]string, val string) (int, bool)

Find searches for val in the slice pointed to by slice. It returns the index of the first match and true when found. If slice is nil or val is not present, it returns -1 and false.

func FindInSequence added in v1.134.0

func FindInSequence[E any](elements iter.Seq[E], predicate Predicate[E]) (int, bool)

FindInSequence searches elements (a sequence) for the first item that satisfies the predicate. It returns the zero-based index of the matching element and true when a match is found. If elements is nil or no match exists, it returns -1 and false.

func FindInSequenceRef added in v1.147.0

func FindInSequenceRef[E any](elements iter.Seq[E], predicate PredicateRef[E]) (int, bool)

FindInSequenceRef behaves like FindInSequence but accepts a predicate that operates on element references.

func FindInSlice added in v1.49.0

func FindInSlice(strict bool, slice []string, val ...string) (int, bool)

FindInSlice checks whether any of the provided val arguments exist in slice. It returns the index of the first match and true if found. When strict is true, matching uses exact string equality. When strict is false, matching is case-insensitive and ignores surrounding whitespace.

If no values are provided or the slice is empty, the function returns -1 and false.

func FlatMap added in v1.161.0

func FlatMap[S ~[]E, E any, T any](slice S, mapper MapFunc[E, []T]) []T

FlatMap maps each element and concatenates the returned slices.

This is useful when each input element expands into zero or more output elements, such as splitting strings, flattening nested values, or projecting structs into multiple derived values.

Reference documentation:

func FlatMapRef added in v1.161.0

func FlatMapRef[S ~[]E, E any, T any](slice S, mapper MapRefFunc[E, []T]) []T

FlatMapRef behaves like FlatMap but accepts a reference-based mapper.

func FlatMapRefSequence added in v1.161.0

func FlatMapRefSequence[E any, T any](sequence iter.Seq[E], mapper MapRefFunc[E, []T]) []T

FlatMapRefSequence behaves like FlatMapSequence but accepts a reference-based mapper.

func FlatMapSequence added in v1.161.0

func FlatMapSequence[E any, T any](sequence iter.Seq[E], mapper MapFunc[E, []T]) []T

FlatMapSequence maps each sequence element and concatenates the returned slices.

func ForAll added in v1.138.0

func ForAll[S ~[]E, E any](s S, f OperationFunc[E]) error

ForAll invokes f on every element of the provided slice. Any non-EOF errors returned by f are collected and aggregated into a single error returned to the caller. If f returns EOF, iteration stops immediately.

func ForAllRef added in v1.147.0

func ForAllRef[S ~[]E, E any](s S, f OperationRefFunc[E]) error

ForAllRef behaves like ForAll but applies f to references of elements.

func ForAllSequence added in v1.138.0

func ForAllSequence[T any](s iter.Seq[T], f OperationFunc[T]) error

ForAllSequence invokes f for every element of s (a sequence). Non-EOF errors are wrapped and aggregated; EOF causes immediate termination.

func ForAllSequenceRef added in v1.147.0

func ForAllSequenceRef[T any](s iter.Seq[T], f OperationRefFunc[T]) error

ForAllSequenceRef behaves like ForAllSequence but adapts a reference-based operation.

func ForEach added in v1.134.0

func ForEach[S ~[]E, E any](s S, f OperationWithoutErrorFunc[E])

ForEach invokes f on every element of the provided slice. Any error returned by f is ignored.

func ForEachRef added in v1.147.0

func ForEachRef[S ~[]E, E any](s S, f OperationWithoutErrorRefFunc[E])

ForEachRef invokes f on every element of the provided slice, passing a reference.

func ForEachValues added in v1.134.0

func ForEachValues[E any](f func(E), values ...E)

ForEachValues invokes f for each value passed in values.

func FrequenciesBy added in v1.161.0

func FrequenciesBy[S ~[]E, E any, K comparable](slice S, keyFunc KeyFunc[E, K]) map[K]int

FrequenciesBy counts how often each derived key occurs in slice.

This is useful for building histograms or frequency tables, for example counting values by lowercase normalisation, status, or category.

Reference documentation:

func FrequenciesByRef added in v1.161.0

func FrequenciesByRef[S ~[]E, E any, K comparable](slice S, keyFunc KeyRefFunc[E, K]) map[K]int

FrequenciesByRef behaves like FrequenciesBy but accepts a reference-based key function.

func FrequenciesByRefSequence added in v1.161.0

func FrequenciesByRefSequence[E any, K comparable](sequence iter.Seq[E], keyFunc KeyRefFunc[E, K]) map[K]int

FrequenciesByRefSequence behaves like FrequenciesBySequence but accepts a reference-based key function.

func FrequenciesBySequence added in v1.161.0

func FrequenciesBySequence[E any, K comparable](sequence iter.Seq[E], keyFunc KeyFunc[E, K]) map[K]int

FrequenciesBySequence counts how often each derived key occurs in a sequence.

func GenericRemove added in v1.4.0

func GenericRemove(equal func(string, string) bool, slice []string, val ...string) []string

GenericRemove looks for elements in a slice using the equal function. If they're found, it will remove them from the slice.

func GroupBy added in v1.161.0

func GroupBy[S ~[]E, E any, K comparable](slice S, keyFunc KeyFunc[E, K]) map[K][]E

GroupBy groups slice elements by the key returned by keyFunc.

This is useful when a collection should be partitioned into buckets, such as grouping users by role, strings by length, or values by status.

Reference documentation:

func GroupByRef added in v1.161.0

func GroupByRef[S ~[]E, E any, K comparable](slice S, keyFunc KeyRefFunc[E, K]) map[K][]E

GroupByRef behaves like GroupBy but accepts a reference-based key function.

func GroupByRefSequence added in v1.161.0

func GroupByRefSequence[E any, K comparable](sequence iter.Seq[E], keyFunc KeyRefFunc[E, K]) map[K][]E

GroupByRefSequence behaves like GroupBySequence but accepts a reference-based key function.

func GroupBySequence added in v1.161.0

func GroupBySequence[E any, K comparable](sequence iter.Seq[E], keyFunc KeyFunc[E, K]) map[K][]E

GroupBySequence groups sequence elements by the key returned by keyFunc.

This is useful when grouping should happen during lazy iteration rather than after collecting values into a slice.

func In added in v1.156.0

func In[Slice ~[]E, E any](s Slice, v E, m ...MatchFunc[E]) bool

In reports whether any element in s matches v using any of the provided match functions. Match functions that return an error are treated as non-matches.

func InRef added in v1.156.0

func InRef[Slice ~[]E, E any](s Slice, v *E, m ...MatchRefFunc[E]) bool

InRef behaves like In but uses reference-based match functions and a reference value.

func InSequence added in v1.156.0

func InSequence[E any](s iter.Seq[E], v E, m ...MatchFunc[E]) bool

InSequence reports whether any element in s matches v using any of the provided match functions. Match functions that return an error are treated as non-matches.

func InSequenceRef added in v1.156.0

func InSequenceRef[E any](s iter.Seq[E], v *E, m ...MatchRefFunc[E]) bool

InSequenceRef behaves like InSequence but uses reference-based match functions and a reference value.

func Intersection added in v1.134.0

func Intersection[T comparable](slice1, slice2 []T) []T

Intersection returns the distinct values common to slice1 and slice2. The order of elements is not guaranteed.

func Map added in v1.110.0

func Map[T1 any, T2 any](s []T1, f MapFunc[T1, T2]) []T2

Map applies f to each element of s and returns a slice with the results.

This is useful for eagerly transforming one slice into another, for example converting IDs to strings, extracting names from structs, or normalising values before further processing.

func MapRef added in v1.147.0

func MapRef[T1 any, T2 any](s []T1, f MapRefFunc[T1, T2]) []T2

MapRef applies a reference-based mapping function to s and returns the mapped slice.

This is useful when eager slice transformation should reuse logic written for pointer-oriented mappers.

func MapRefWithError added in v1.147.0

func MapRefWithError[T1 any, T2 any](s []T1, f MapRefWithErrorFunc[T1, T2]) (result []T2, err error)

MapRefWithError applies a reference-based mapper that may return an error. If the mapper returns nil or an error, processing stops and an error is returned.

This is useful when eager slice transformation should stop as soon as an invalid or missing mapped value is encountered.

func MapSequence added in v1.134.0

func MapSequence[T1 any, T2 any](s iter.Seq[T1], f MapFunc[T1, T2]) iter.Seq[T2]

MapSequence maps each element of s using f and returns a sequence of mapped values.

This is useful when data should be transformed lazily, for example while streaming over iterators or when chaining other sequence operations.

Reference documentation:

func MapSequenceRef added in v1.147.0

func MapSequenceRef[T1 any, T2 any](s iter.Seq[T1], f MapRefFunc[T1, T2]) iter.Seq[T2]

MapSequenceRef maps a sequence by applying a reference-based mapper to each element.

This is useful when values are consumed lazily and the mapping logic is already written against pointers.

func MapSequenceRefWithError added in v1.147.0

func MapSequenceRefWithError[T1 any, T2 any](s iter.Seq[T1], f MapRefWithErrorFunc[T1, T2]) iter.Seq[T2]

MapSequenceRefWithError maps a sequence using a reference-based mapper which may return an error. Mapping stops if the mapper returns an error, returns nil, or if the consumer declines the yielded value.

This is useful when lazy iteration and pointer-oriented mapping need to be combined with validation or other error-producing transformations.

func MapSequenceWithError added in v1.134.0

func MapSequenceWithError[T1 any, T2 any](s iter.Seq[T1], f MapWithErrorFunc[T1, T2]) iter.Seq[T2]

MapSequenceWithError maps each element of s using f, which may return an error. Mapping stops if f returns an error or if the consumer declines the yielded value.

func MapWithError added in v1.124.0

func MapWithError[T1 any, T2 any](s []T1, f MapWithErrorFunc[T1, T2]) (result []T2, err error)

MapWithError applies f to each element of s where f may return an error. If an error occurs, processing stops and the error is returned.

func Match added in v1.127.0

func Match[E any](e E, matches ...FilterFunc[E]) bool

Match returns true if any of the provided match predicates return true for e.

func MatchAll added in v1.127.0

func MatchAll[E any](e E, matches ...FilterFunc[E]) bool

MatchAll returns true only if all the provided match predicates return true for e.

func Negate added in v1.80.0

func Negate(values ...bool) []bool

Negate returns the slice with contrary values.

func OneHot added in v1.80.0

func OneHot(values ...bool) bool

OneHot returns true if one, and only one, of the supplied values is true. See https://en.wikipedia.org/wiki/One-hot

func Or added in v1.80.0

func Or(values ...bool) bool

Or performs an 'or' operation on an array of booleans.

func PairSplitModeStrings added in v1.150.0

func PairSplitModeStrings() []string

PairSplitModeStrings returns a slice of all String values of the enum

func ParseCommaSeparatedList

func ParseCommaSeparatedList(input string) []string

ParseCommaSeparatedList returns the list of string separated by a comma

func ParseCommaSeparatedListOfPairsToMap added in v1.96.0

func ParseCommaSeparatedListOfPairsToMap(input, pairSeparator string) (pairs map[string]string, err error)

ParseCommaSeparatedListOfPairsToMap returns a map of key value pairs from a string containing a comma separated list of pairs using pairSeparator to separate between keys and values e.g. key1=value1,key2=value2

func ParseCommaSeparatedListToMap added in v1.71.0

func ParseCommaSeparatedListToMap(input string) (pairs map[string]string, err error)

ParseCommaSeparatedListToMap returns a map of key value pairs from a string containing a comma separated list

func ParseListWithCleanup added in v1.9.0

func ParseListWithCleanup(input string, sep string) (newS []string)

ParseListWithCleanup splits a string into a list like strings.Split but also removes any whitespace surrounding the different items for example, ParseListWithCleanup("a, b , c", ",") returns []{"a","b","c"}

func ParseListWithCleanupKeepBlankLines added in v1.12.0

func ParseListWithCleanupKeepBlankLines(input string, sep string) (newS []string)

ParseListWithCleanupKeepBlankLines splits a string into a list like strings.Split but also removes any whitespace surrounding the different items unless the entire item is whitespace in which case it is converted to an empty string. For example, ParseListWithCleanupKeepBlankLines("a, b , c", ",") returns []{"a","b","c"} ParseListWithCleanupKeepBlankLines("a, b , , c", ",") returns []{"a","b", "", "c"}

func Random added in v1.161.0

func Random[S ~[]E, E any](slice S) (E, bool)

Random returns a random element from the slice.

This is useful when one arbitrary element should be sampled from a slice, such as choosing a test case, backend, worker, or candidate value.

The first return value is the selected element. The second return value reports whether an element could be selected. If the slice is empty, Random returns the zero value of E and false.

Reference documentation:

func Range added in v1.120.0

func Range[T safecast.IConvertible](start, stop T, step *T) (result []T)

Range returns a slice of integers similar to Python's built-in range(). https://docs.python.org/2/library/functions.html#range

Note: The stop value is always exclusive.

func RangeSequence added in v1.136.0

func RangeSequence[T safecast.IConvertible](start, stop T, step *T) iter.Seq[T]

RangeSequence returns an iterator over a range

func Reduce added in v1.110.0

func Reduce[T1, T2 any](s []T1, accumulator T2, f ReduceFunc[T1, T2]) T2

Reduce folds over the slice s using f, starting with accumulator.

This is useful when a collection needs to be collapsed into one value, for example summing values, building a lookup map, or computing a derived result from many elements.

Reference documentation:

func ReducesSequence added in v1.134.0

func ReducesSequence[T1, T2 any](s iter.Seq[T1], accumulator T2, f ReduceFunc[T1, T2]) T2

ReducesSequence folds over a sequence using f, starting with accumulator.

This is the sequence-oriented counterpart to Reduce and is useful when values are consumed lazily rather than from a prebuilt slice, such as values flowing through iterators or other on-demand pipelines.

Reference documentation:

func Reject added in v1.110.0

func Reject[S ~[]E, E any](s S, f FilterFunc[E]) S

Reject returns elements for which f returns false (the inverse of Filter). This returns a new slice rather than modifying the input.

func RejectRef added in v1.147.0

func RejectRef[S ~[]E, E any](s S, f FilterRefFunc[E]) S

RejectRef behaves like Reject but accepts a reference-based predicate.

func RejectSequence added in v1.134.0

func RejectSequence[E any](s iter.Seq[E], f FilterFunc[E]) iter.Seq[E]

RejectSequence returns a sequence that yields elements for which f returns false.

This is useful when the complement of a filter condition should be applied in a lazy pipeline.

func Remove added in v1.4.0

func Remove(slice []string, val ...string) []string

Remove looks for elements in a slice. If they're found, it will remove them.

func Shuffle added in v1.161.0

func Shuffle[S ~[]E, E any](slice S) S

Shuffle returns a shuffled copy of the slice.

This is useful when the same elements should be processed in a random order without mutating the original slice.

Reference documentation:

func StrictRefMatch added in v1.156.0

func StrictRefMatch[E comparable](a, b *E) (bool, error)

StrictRefMatch reports whether two references are equal using field.Equal.

func SymmetricDifference added in v1.134.0

func SymmetricDifference[T comparable](slice1, slice2 []T) []T

SymmetricDifference returns distinct values that are present in either slice1 or slice2 but not in both.

func Union added in v1.134.0

func Union[T comparable](slice1, slice2 []T) []T

Union returns the union of slice1 and slice2, containing only unique values. The order of elements is not guaranteed.

func Unique added in v1.134.0

func Unique[T comparable](s iter.Seq[T]) []T

Unique returns the distinct values from the provided sequence. The order of elements is not guaranteed.

func UniqueBy added in v1.161.0

func UniqueBy[S ~[]E, E any, K comparable](slice S, keyFunc KeyFunc[E, K]) S

UniqueBy returns the first occurrence of each unique derived key.

This is useful when values should remain in input order while duplicates are removed based on a derived property such as an ID, normalised string, or computed category.

Reference documentation:

func UniqueByRef added in v1.161.0

func UniqueByRef[S ~[]E, E any, K comparable](slice S, keyFunc KeyRefFunc[E, K]) S

UniqueByRef behaves like UniqueBy but accepts a reference-based key function.

func UniqueByRefSequence added in v1.161.0

func UniqueByRefSequence[E any, K comparable](sequence iter.Seq[E], keyFunc KeyRefFunc[E, K]) []E

UniqueByRefSequence behaves like UniqueBySequence but accepts a reference-based key function.

func UniqueBySequence added in v1.161.0

func UniqueBySequence[E any, K comparable](sequence iter.Seq[E], keyFunc KeyFunc[E, K]) []E

UniqueBySequence returns the first occurrence of each unique derived key from a sequence.

func UniqueEntries added in v1.75.0

func UniqueEntries[T comparable](slice []T) []T

UniqueEntries returns a slice containing the distinct values from the provided slice. The order of elements is not guaranteed.

func Xor added in v1.80.0

func Xor(values ...bool) bool

Xor performs a `xor` on an array of booleans. This behaves like an XOR gate; it returns true if the number of true values is odd, and false if the number of true values is zero or even.

Types

type Conditions added in v1.80.0

type Conditions []bool

func NewConditions added in v1.80.0

func NewConditions(capacity int) Conditions

NewConditions creates a set of conditions.

func NewConditionsFromValues added in v1.80.0

func NewConditionsFromValues(conditions ...bool) Conditions

NewConditionsFromValues creates a set of conditions.

func (*Conditions) Add added in v1.80.0

func (c *Conditions) Add(conditions ...bool) Conditions

Add adds conditions and returns itself.

func (*Conditions) All added in v1.80.0

func (c *Conditions) All() bool

All returns true if all conditions are true.

func (*Conditions) And added in v1.80.0

func (c *Conditions) And() bool

And performs an `and` operation on all conditions

func (*Conditions) Any added in v1.80.0

func (c *Conditions) Any() bool

Any returns true if there is at least one condition which is true.

func (*Conditions) Concat added in v1.80.0

func (c *Conditions) Concat(more *Conditions) Conditions

Concat concatenates conditions and returns itself.

func (*Conditions) Contains added in v1.80.0

func (c *Conditions) Contains(condition bool) bool

Contains returns whether the conditions collection contains the value of a condition.

func (*Conditions) ForEach added in v1.80.0

func (c *Conditions) ForEach(each func(bool) error) error

ForEach will execute function each() on every condition unless an error is returned and will end add this point.

func (*Conditions) Negate added in v1.80.0

func (c *Conditions) Negate() Conditions

Negate returns a new set of conditions with negated values.

func (*Conditions) OneHot added in v1.80.0

func (c *Conditions) OneHot() bool

OneHot performs an `OneHot` operation on all conditions

func (*Conditions) Or added in v1.80.0

func (c *Conditions) Or() bool

Or performs an `Or` operation on all conditions

func (*Conditions) Xor added in v1.80.0

func (c *Conditions) Xor() bool

Xor performs a `Xor` operation on all conditions

type FilterFunc added in v1.120.0

type FilterFunc[E any] func(E) bool

FilterFunc defines a function that evaluates a value and returns true when the value satisfies the condition.

Filter predicates are useful for selecting only the elements that should be kept, such as enabled records, matching names, valid values, or items within a threshold.

func OppositeFunc added in v1.134.0

func OppositeFunc[E any](f FilterFunc[E]) FilterFunc[E]

OppositeFunc returns a predicate that negates the result of f.

type FilterRefFunc added in v1.147.0

type FilterRefFunc[E any] func(*E) bool

FilterRefFunc defines a function that evaluates a pointer to a value and returns true when the referenced value satisfies the condition.

Reference-based predicates are useful when the predicate should be able to work with optional values, share logic with other pointer-oriented helpers, or avoid copying larger element values.

type KeyFunc added in v1.161.0

type KeyFunc[T1 any, T2 comparable] = MapFunc[T1, T2]

KeyFunc defines a function that maps an element to a comparable key.

Key functions are useful when values need to be grouped, counted, or deduplicated based on a derived identity such as an ID, category, or normalised form.

type KeyRefFunc added in v1.161.0

type KeyRefFunc[T1 any, T2 comparable] func(*T1) T2

KeyRefFunc defines a function that maps a pointer to a value to a comparable key.

Reference-based key functions are useful when grouping or counting logic is naturally written against optional or pointer-based values.

type MapFunc added in v1.120.0

type MapFunc[T1, T2 any] func(T1) T2

MapFunc defines a function that maps a value of type T1 to type T2.

Mapping functions are useful for transforming collections from one shape into another, such as extracting a field, converting one type to another, or preparing values for serialisation or display.

func IdentityMapFunc added in v1.120.0

func IdentityMapFunc[T any]() MapFunc[T, T]

IdentityMapFunc returns a mapping function that returns its input unchanged.

type MapRefFunc added in v1.147.0

type MapRefFunc[T1, T2 any] func(*T1) *T2

MapRefFunc defines a mapping function that accepts a pointer to T1 and returns a pointer to T2.

Reference-based mappers are useful when the mapping logic naturally works on pointers, when a value may be optional, or when larger values should not be copied unnecessarily.

type MapRefWithErrorFunc added in v1.147.0

type MapRefWithErrorFunc[T1, T2 any] func(*T1) (*T2, error)

MapRefWithErrorFunc defines a reference-based mapping function that may return an error.

type MapWithErrorFunc added in v1.124.0

type MapWithErrorFunc[T1, T2 any] func(T1) (T2, error)

MapWithErrorFunc defines a mapping function that may return an error.

type MatchFunc added in v1.156.0

type MatchFunc[E any] func(E, E) (bool, error)

MatchFunc compares two values of type E and reports whether they match. It may return an error if the comparison requires additional processing, such as compiling or evaluating a regular expression.

var StringCaseInsensitiveMatch MatchFunc[string] = func(a, b string) (bool, error) { return strings.EqualFold(a, b), nil }

StringCaseInsensitiveMatch reports whether two strings are equal but ignoring their case.

var StringCleanCaseInsensitiveMatch MatchFunc[string] = func(a, b string) (bool, error) {
	return StringCaseInsensitiveMatch(strings.TrimSpace(a), strings.TrimSpace(b))
}

StringCleanCaseInsensitiveMatch reports whether two strings are equal after trimming surrounding whitespace and ignoring their case.

var StringCleanMatch MatchFunc[string] = func(a, b string) (bool, error) { return StringMatch(strings.TrimSpace(a), strings.TrimSpace(b)) }

StringCleanMatch reports whether two strings are exactly equal after trimming surrounding whitespace.

var StringMatch MatchFunc[string] = func(a, b string) (bool, error) { return a == b, nil }

StringMatch reports whether two strings are exactly equal.

var StringRegexMatch MatchFunc[string] = regexp.MatchString

StringRegexMatch reports whether a string matches the provided regular expression pattern. the pattern being the first argument.

type MatchRefFunc added in v1.156.0

type MatchRefFunc[E any] func(*E, *E) (bool, error)

MatchRefFunc compares two references to values of type E and reports whether they match. It may return an error if the comparison requires additional processing before the match can be determined.

type OperationFunc added in v1.147.0

type OperationFunc[E any] func(E) error

OperationFunc defines an operation on a value that may return an error.

type OperationRefFunc added in v1.147.0

type OperationRefFunc[E any] func(*E) error

OperationRefFunc defines an operation on a pointer to a value that may return an error.

type OperationWithoutErrorFunc added in v1.147.0

type OperationWithoutErrorFunc[E any] func(E)

OperationWithoutErrorFunc defines an operation on a value that does not return an error.

type OperationWithoutErrorRefFunc added in v1.147.0

type OperationWithoutErrorRefFunc[E any] func(*E)

OperationWithoutErrorRefFunc defines an operation on a pointer that does not return an error.

type PairOption added in v1.150.0

type PairOption func(*PairOptions) *PairOptions

PairOption applies configuration to PairOptions for pair parsing behaviour.

func WithPairSeparator added in v1.150.0

func WithPairSeparator(sep string) PairOption

WithPairSeparator sets the separator used to split key/value pair entries.

func WithPairSplitMode added in v1.150.0

func WithPairSplitMode(mode PairSplitMode) PairOption

WithPairSplitMode sets how a pair entry is split into key and value.

type PairOptions added in v1.150.0

type PairOptions struct {
	PairSeparator string
	// contains filtered or unexported fields
}

type PairSplitMode added in v1.150.0

type PairSplitMode int
const (
	// SplitAllMatch splits each pair item on all matches of the separator.
	// The item is valid only if it resolves to exactly two non-empty parts after cleanup.
	SplitAllMatch PairSplitMode = iota
	// SplitFirstMatch splits each pair item only on the first match of the separator.
	// This allows separator characters to remain in the value portion.
	SplitFirstMatch
)

func PairSplitModeString added in v1.150.0

func PairSplitModeString(s string) (PairSplitMode, error)

PairSplitModeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func PairSplitModeValues added in v1.150.0

func PairSplitModeValues() []PairSplitMode

PairSplitModeValues returns all values of the enum

func (PairSplitMode) IsAPairSplitMode added in v1.150.0

func (i PairSplitMode) IsAPairSplitMode() bool

IsAPairSplitMode returns "true" if the value is listed in the enum definition. "false" otherwise

func (PairSplitMode) MarshalJSON added in v1.150.0

func (i PairSplitMode) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for PairSplitMode

func (PairSplitMode) MarshalText added in v1.150.0

func (i PairSplitMode) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface for PairSplitMode

func (PairSplitMode) MarshalYAML added in v1.150.0

func (i PairSplitMode) MarshalYAML() (interface{}, error)

MarshalYAML implements a YAML Marshaler for PairSplitMode

func (PairSplitMode) String added in v1.150.0

func (i PairSplitMode) String() string

func (*PairSplitMode) UnmarshalJSON added in v1.150.0

func (i *PairSplitMode) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for PairSplitMode

func (*PairSplitMode) UnmarshalText added in v1.150.0

func (i *PairSplitMode) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for PairSplitMode

func (*PairSplitMode) UnmarshalYAML added in v1.150.0

func (i *PairSplitMode) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements a YAML Unmarshaler for PairSplitMode

type Predicate added in v1.134.0

type Predicate[E any] = FilterFunc[E]

Predicate is an alias for FilterFunc to express boolean tests.

type PredicateRef added in v1.147.0

type PredicateRef[E any] = FilterRefFunc[E]

PredicateRef is an alias for FilterRefFunc to express boolean tests on references.

type ReduceFunc added in v1.120.0

type ReduceFunc[T1, T2 any] func(T2, T1) T2

ReduceFunc defines a reducer that combines an accumulator and an element to produce a new accumulator.

Reducers are useful for aggregating collections into a single result, such as totals, counts, grouped values, flattened structures, or derived summary objects.

type ReduceRefFunc added in v1.147.0

type ReduceRefFunc[T1, T2 any] func(*T2, *T1) *T2

ReduceRefFunc defines a reducer that operates on references.

Reference-based reducers are useful when the accumulator or elements are more naturally handled as pointers, for example when building up mutable state or sharing reducer logic with other pointer-oriented helpers.

Directories

Path Synopsis
Package pagination provides a ways to iterate over collections.
Package pagination provides a ways to iterate over collections.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL