collection

package
v0.0.0-...-86a7930 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2025 License: BSD-2-Clause Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllExist

func AllExist[T comparable](superset, subset []T) bool

AllExist checks if all elements of a subset slice are present in a superset slice.

func CurriedFilter

func CurriedFilter[T any](predicate func(T) bool) func([]T) []T

CurriedFilter returns a function that, when given a slice, filters it based on the provided predicate. This is a curried version of the Filter function.

func CurriedMap

func CurriedMap[T1, T2 any](fnc func(T1) T2) func([]T1) []T2

CurriedMap returns a function that, when given a slice, applies the provided function to each element of the slice. This is a curried version of the Map function.

func CurriedPartition

func CurriedPartition[T any](predicate func(T) bool) func([]T) ([]T, []T)

CurriedPartition returns a function that, when given a slice, partitions it based on the provided predicate. This is a curried version of the Partition function.

func CurriedPartitionTuple2

func CurriedPartitionTuple2[T any](
	predicate func(T) bool,
) func([]T) Tuple2[[]T, []T]

CurriedPartitionTuple2 returns a function that, when given a slice, partitions it based on the provided predicate and returns the result as a Tuple2. This is a curried version of the PartitionTuple2 function.

func CurriedTFold

func CurriedTFold[A, B, R any](
	folder func(Tuple2[A, B]) R,
) func(Tuple2[A, B]) R

CurriedFold returns a function that applies the folder function to a Tuple2, returning a result of type R. This is a curried version of Fold.

func Filter

func Filter[T any](slice []T, predicate func(T) bool) []T

Filter returns a new slice containing all elements that satisfy the predicate.

func Find

func Find[T any](slice []T, predicate func(T) bool) (*T, bool)

Find searches for an element in a slice that satisfies the predicate. It returns a pointer to the first element found and true, or nil and false if no element is found.

func Include

func Include[T cmp.Ordered](slice []T, element T) bool

Include determines whether the given element is present in the slice. The slice is sorted before searching.

func IsEmpty

func IsEmpty[T any](slice []T) bool

IsEmpty checks if a slice is empty (has zero elements). Returns true if the slice is nil or has no elements, false otherwise.

func Map

func Map[T1, T2 any](slc []T1, fnc func(T1) T2) []T2

Map returns the slice obtained after applying the given function over every element in the given slice.

func MapSeq

func MapSeq[T1, T2 any](seq iter.Seq[T1], fn func(T1) T2) iter.Seq[T2]

MapSeq transforms an iter.Seq to another iter.Seq by applying the given function to each element in the sequence.

func Partition

func Partition[T any](slice []T, predicate func(T) bool) ([]T, []T)

Partition splits a slice into two slices based on a predicate function. The first returned slice contains all elements for which the predicate returns true, and the second contains all elements for which the predicate returns false.

func RemoveStringItems

func RemoveStringItems(slice []string, items ...string) []string

RemoveStringItems removes elements from a that are present in items.

func Sorted

func Sorted[T cmp.Ordered](slice []T) []T

Sorted returns a new slice containing the elements of the input slice sorted in ascending order. The original slice is not modified.

func TFold

func TFold[A, B, R any](
	tup Tuple2[A, B],
	folder func(Tuple2[A, B]) R,
) R

Fold applies a folding function to each element of a slice along with an accumulator value, returning a result of type R as determined by the folder function. The accumulator is passed as part of a Tuple2 structure.

Types

type Tuple2

type Tuple2[T1, T2 any] struct {
	First  T1
	Second T2
}

Tuple2 represents a pair of values with independent types. It's useful for functions that need to return two values of different types.

func NewTuple2

func NewTuple2[T1, T2 any](first T1, second T2) Tuple2[T1, T2]

NewTuple2 creates a new Tuple2 with the given values.

func PartitionTuple2

func PartitionTuple2[T any](
	slice []T,
	predicate func(T) bool,
) Tuple2[[]T, []T]

PartitionTuple2 splits a slice into two parts based on a predicate function and returns them as a Tuple2. The First field contains all elements for which the predicate returns true, and the Second field contains all elements for which the predicate returns false.

func SliceToTuple2

func SliceToTuple2[T1, T2 any](slice []any) Tuple2[T1, T2]

SliceToTuple2 converts the first two elements of a slice to a Tuple2. If the slice has fewer than two elements, it uses zero values for the missing elements.

Jump to

Keyboard shortcuts

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