collection

package
v1.147.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: Apache-2.0 Imports: 12 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 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 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.

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.

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.

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.

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 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 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 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 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.

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.

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.

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.

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.

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.

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 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 Range added in v1.120.0

func Range[T safecast.IConvertable](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.IConvertable](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.

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.

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.

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 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 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.

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.

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.

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.

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 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 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.

type ReduceRefFunc added in v1.147.0

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

ReduceRefFunc defines a reducer that operates on references.

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