sliceutil

package
v1.0.16 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](s []T, f func(T) bool) bool

All returns true if all elements in s satisfy the predicate f. If s is empty or nil, All returns true.

func Any

func Any[T any](s []T, f func(T) bool) bool

Any returns true if any element in the slice satisfies the predicate, false otherwise. If the slice is empty or nil, Any returns false.

func Chunks

func Chunks[T any](ts []T, size int) [][]T

Chunks returns a slice of chunks of the input slice, each of given size at most.

func ChunksToSeq

func ChunksToSeq[T any](ts []T, size int) iter.Seq[[]T]

ChunksToSeq returns a sequence of chunks of the input slice, each of given size at most.

func Count

func Count[T any](s []T, f func(T) bool) int

Count returns the number of elements in the slice that satisfy the predicate f.

func Filter

func Filter[T any](s []T, f func(T) bool) []T

Filter returns a new slice containing only the elements of slice 's' that satisfy the predicate f.

func FilterInPlace

func FilterInPlace[T any](s []T, f func(T) bool) []T

FilterInPlace removes all elements from slice 's' that do not satisfy the predicate f and returns the modified slice.

func FilterToSeq

func FilterToSeq[T any](s []T, f func(T) bool) iter.Seq[T]

FilterToSeq returns a new sequence containing only the elements of slice 's' that satisfy the predicate f.

func Find

func Find[T any](s []T, f func(T) bool) (int, T)

Find returns the index and the element of the first element in the slice that satisfies the predicate f. If no element satisfies the predicate, Find returns -1 and the zero value of the element type.

func FindPtr added in v1.0.13

func FindPtr[T any](s []T, f func(*T) bool) *T

FindPtr returns the pointer to the first element in the slice that satisfies the predicate f. If no element satisfies the predicate, FindPtr returns nil.

func GoInChunks

func GoInChunks[T any](ts []T, size int, f func([]T) error) error

GoInChunks calls the given function for each chunk of the input slice, each of given size at most.

func GroupBy

func GroupBy[T any, K comparable](slice []T, key func(T) K) map[K][]T

GroupBy groups elements of slice by key K calculated from the slice elements.

func GroupByKV added in v1.0.5

func GroupByKV[T any, K comparable, V any](slice []T, kv func(T) (K, V)) map[K][]V

GroupByKV groups values V by key K where V and K are calculated from the slice elements.

func Map

func Map[T any, U any](is []T, f func(T) U) []U

Map applies a function to each element of a slice and returns a new slice.

func MapMinMax added in v1.0.12

func MapMinMax[T any, K cmp.Ordered](s []T, f func(T) K) (K, K)

MapMinMax applies a function to each element of the slice and finds the minimum and maximum values based on the results of that function.

func MapToSeq

func MapToSeq[T any, U any](is []T, f func(T) U) iter.Seq[U]

MapToSeq applies a function to each element of a slice and returns a new sequence.

func MapUnique

func MapUnique[T any, U comparable](is []T, f func(T) U) []U

MapUnique applies a function to each element of a slice and returns a new slice with unique elements.

func MapUniqueToSeq

func MapUniqueToSeq[T any, U comparable](is []T, f func(T) U) iter.Seq[U]

MapUniqueToSeq applies a function to each element of a slice and returns a new sequence with unique elements.

func MinMax added in v1.0.12

func MinMax[T any](s []T, compare func(T, T) int) (T, T)

MinMax finds minimum and maximum elements in a slice.

func Partition

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

Partition returns two slices, the first slice contains all elements that satisfy the predicate, and the second slice contains all elements that do not satisfy the predicate.

func PartitionConsEq

func PartitionConsEq[T any](slice []T, eq func(a, b T) bool) [][]T

PartitionConsEq returns a slice of slices of consecutive equal elements.

func PartitionConsEqToSeq

func PartitionConsEqToSeq[T any](slice []T, eq func(a, b T) bool) iter.Seq[[]T]

PartitionConsEqToSeq returns a sequence of slices of consecutive equal elements.

func PartitionToSeq

func PartitionToSeq[T any](slice []T, predicate func(T) bool) (trueGroup iter.Seq[T], falseGroup iter.Seq[T])

PartitionToSeq returns two sequences, the first sequence contains all elements that satisfy the predicate, and the second sequence contains all elements that do not satisfy the predicate.

func Reduce

func Reduce[T any, A any](s []T, a A, f func(T, A) A) A

Reduce applies a function against an accumulator and each element in the slice (from left to right) to reduce it to a single value.

func Sort

func Sort[T any](s []T, compare func(T, T) int)

Sort sorts a slice using a compare function.

func Sorted

func Sorted[T any](s []T, compare func(T, T) int) []T

Sorted returns a sorted copy of a slice using a compare function.

Types

This section is empty.

Jump to

Keyboard shortcuts

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