slices

package
v6.0.0-...-02ba6c8 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: MPL-2.0 Imports: 2 Imported by: 0

README

Generic Slice Functions

Complements Go standard library slices package.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var WithReturnFirstMatch = func(o *FinderOptions) {
	o.returnFirstMatch = true
}

WithReturnFirstMatch is a finder option to enable paginated operations to short circuit after the first filter match

This option should only be used when only a single match will ever be returned from the specified filter.

Functions

func All

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

All returns `true` if the filter function `f` returns `true` for all items in slice `s`.

func Any

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

Any returns `true` if the filter function `f` returns `true` for any item in slice `s`.

func AppendSeqWithError

func AppendSeqWithError[S ~[]E, E any](s S, seq iter.Seq2[E, error]) (S, error)

AppendSeqWithError appends the values from seq to the slice and returns the extended slice. The first non-nil error in seq is returned. If seq is empty, the result preserves the nilness of s.

func AppendUnique

func AppendUnique[S ~[]E, E comparable](s S, vs ...E) S

AppendUnique appends unique (not already in the slice) values to a slice.

func AppliedToEach

func AppliedToEach[S ~[]E, E any, T any](s S, f func(E) T) iter.Seq[T]

AppliedToEach returns an iterator that yields the slice elements transformed by the function `f`.

func ApplyToAll

func ApplyToAll[S ~[]E1, E1, E2 any](s S, f func(E1) E2) []E2

ApplyToAll returns a new slice containing the results of applying the function `f` to each element of the original slice `s`.

func ApplyToAllWithError

func ApplyToAllWithError[S ~[]E1, E1, E2 any](s S, f func(E1) (E2, error)) ([]E2, error)

func BackwardValues

func BackwardValues[Slice ~[]E, E any](s Slice) iter.Seq[E]

BackwardValues returns an iterator that yields the slice elements in reverse order. It is a values-only equivalent of `slices.Backward`.

func CollectWithError

func CollectWithError[E any](seq iter.Seq2[E, error]) ([]E, error)

CollectWithError collects values from seq into a new slice and returns it. The first non-nil error in seq is returned. If seq is empty, the result is nil.

func Filter

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

Filter returns a new slice containing all values that return `true` for the filter function `f`.

func IndexOf

func IndexOf[S ~[]any, E comparable](s S, v E) int

IndexOf returns the index of the first occurrence of `v` in `s`, or -1 if not present. This function is similar to the `Index` function in the Go standard `slices` package, the difference being that `s` is a slice of `any` and a runtime type check is made.

func Range

func Range[T signed](start, stop, step T) []T

Range returns a slice of integers from `start` to `stop` (exclusive) using the specified `step`.

func RemoveAll

func RemoveAll[S ~[]E, E comparable](s S, vs ...E) S

RemoveAll removes all occurrences of the specified value `r` from a slice `s`.

func Reverse

func Reverse[S ~[]E, E any](s S) S

Reverse returns a reversed copy of the slice `s`.

func Strings

func Strings[S ~[]E, E Stringable](s S) []string

func Values

func Values[S ~[]*E, E any](s S) []E

Values returns a new slice containing values from the pointers in each element of the original slice `s`.

Types

type FinderOptions

type FinderOptions struct {
	// contains filtered or unexported fields
}

func NewFinderOptions

func NewFinderOptions(optFns []FinderOptionsFunc) *FinderOptions

func (*FinderOptions) ReturnFirstMatch

func (o *FinderOptions) ReturnFirstMatch() bool

type FinderOptionsFunc

type FinderOptionsFunc func(*FinderOptions)

type Predicate

type Predicate[T any] func(T) bool

Predicate represents a predicate (boolean-valued function) of one argument.

func PredicateAnd

func PredicateAnd[T any](predicates ...Predicate[T]) Predicate[T]

PredicateAnd returns a Predicate that evaluates to true if all of the specified predicates evaluate to true.

func PredicateEquals

func PredicateEquals[T comparable](v T) Predicate[T]

PredicateEquals returns a Predicate that evaluates to true if the predicate's argument equals `v`.

func PredicateOr

func PredicateOr[T any](predicates ...Predicate[T]) Predicate[T]

PredicateOr returns a Predicate that evaluates to true if any of the specified predicates evaluate to true.

func PredicateTrue

func PredicateTrue[T any]() Predicate[T]

PredicateTrue returns a Predicate that always evaluates to true.

func PredicateValue

func PredicateValue[T any](predicate Predicate[*T]) Predicate[T]

type Stringable

type Stringable interface {
	~string | ~[]byte | ~[]rune
}

Jump to

Keyboard shortcuts

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