sliceutils

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

Sliceutils

Functional utilities for working with slices in Go, providing a rich set of operations for transforming, filtering, and manipulating slice data.

Features

  • Transformations - Map, filter, and reduce operations
  • Manipulations - Reverse, shuffle, pad, and repeat
  • Predicates - Any, all, unique checks, and sublist testing
  • Combinations - Generate k-combinations and k-covering combinations
  • Folding - Accumulate values with error handling

Supports generic types and provides both in-place and non-destructive variants where applicable.

Documentation

Overview

Package sliceutils provides functional utilities for working with slices in Go, providing a rich set of operations for transforming, filtering, and manipulating slice data.

See README.md for details.

Index

Constants

This section is empty.

Variables

View Source
var ErrArgumentIsNil = errs.New("argument is nil")

Functions

func All

func All[S ~[]T, T any](xs S, predicate func(T) bool) bool

All returns true if all elements in xs satisfy the predicate.

func Any

func Any[S ~[]T, T any](xs S, predicate func(T) bool) bool

Any returns true if any element in xs satisfies the predicate.

func Combinations

func Combinations[S ~[]T, T any](s S, k uint) iter.Seq[S]

Combinations generates all k-combinations of the input slice s.

func ContainsFunc

func ContainsFunc[S ~[]T, T any](xs S, x T, equal func(T, T) bool) bool

ContainsFunc returns true if xs contains the element x using the provided equality function.

func Count

func Count[S ~[]T, T any](xs S, predicate func(T) bool) int

Count returns the number of elements in xs that satisfy the predicate.

func CountUnique

func CountUnique[S ~[]T, T comparable](xs S) int

CountUnique returns the number of unique elements in xs.

func CountUniqueFunc

func CountUniqueFunc[S ~[]T, T any](xs S, equal func(T, T) bool) int

CountUniqueFunc returns the number of unique elements in xs using the provided equality function.

func Fill

func Fill[T any](s []T, x T)

Fill fills the slice s with the value x.

func Filter

func Filter[S ~[]T, T any](xs S, predicate func(T) bool) S

Filter returns a new slice containing only the elements of xs that satisfy the predicate.

func Fold

func Fold[T, U any](f func(acc U, x T) U, initial U, rest ...T) U

Fold reduces the slice rest to a single value by applying the binary function f cumulatively, starting with initial.

func FoldOrError

func FoldOrError[T, U any](f func(acc U, x T) (U, error), initial U, rest ...T) (U, error)

FoldOrError reduces the slice rest to a single value by applying the binary function f cumulatively, starting with initial.

func IsAllUnique

func IsAllUnique[S ~[]T, T comparable](xs S) bool

IsAllUnique returns true if all elements in xs are unique.

func IsAllUniqueFunc

func IsAllUniqueFunc[S ~[]T, T any](xs S, equal func(T, T) bool) bool

IsAllUniqueFunc returns true if all elements in xs are unique using the provided equality function.

func IsSubSet

func IsSubSet[SB, SP ~[]T, T comparable](sub SB, sup SP) bool

IsSubSet returns true if all elements of sub are present in sup.

func IsSubSetFunc

func IsSubSetFunc[SB, SP ~[]T, T any](sub SB, sup SP, equal func(T, T) bool) bool

IsSubSetFunc returns true if all elements of sub are present in sup using the provided equality function.

func IsSuperSet

func IsSuperSet[T comparable](ss, s []T) bool

IsSuperSet returns true if ss contains all elements of s.

func KCoveringCombinations

func KCoveringCombinations[S ~[]T, T any](s S, k uint) iter.Seq[S]

KCoveringCombinations generates all combinations of the input slice s with sizes from k to len(s).

func Map

func Map[TOut any, SIn ~[]TIn, TIn any](in SIn, f func(TIn) TOut) []TOut

Map applies the function f to each element of the input slice in, returning a slice of the output type.

func MapCast

func MapCast[S ~[]TOut, TOut any, SIn ~[]TIn, TIn any](in SIn, f func(TIn) TOut) S

MapCast applies the function f to each element of the input slice in, casting the output slice to the desired type S.

func MapOrError

func MapOrError[SIn ~[]TIn, TIn, TOut any](in SIn, f func(TIn) (TOut, error)) (out []TOut, err error)

MapOrError applies the function f to each element of the input slice in,.

func PadToLeft

func PadToLeft[S ~[]T, T any](xs S, padLength int) S

PadToLeft pads the input slice xs to the left with zero values to reach the desired padLength.

func PadToLeftWith

func PadToLeftWith[S ~[]T, T any](xs S, padLength int, pad T) S

PadToLeftWith pads the input slice xs to the left with the specified pad value to reach the desired padLength.

func PadToRight

func PadToRight[S ~[]T, T any](xs S, padLength int) S

PadToRight pads the input slice xs to the right with zero values to reach the desired padLength.

func PadToRightWith

func PadToRightWith[S ~[]T, T any](xs S, padLength int, pad T) S

PadToRightWith pads the input slice xs to the right with the specified pad value to reach the desired padLength.

func Reduce

func Reduce[S ~[]T, T any](xs S, initial T, f func(T, T) T) T

Reduce reduces the slice xs to a single value by applying the binary function f cumulatively.

func Repeat

func Repeat[S ~[]T, T any](x T, n int) S

Repeat creates a slice of length n, filled with the value x.

func Reverse

func Reverse[S ~[]T, T any](xs S) S

Reverse reverses the input slice in place and returns it.

func Reversed

func Reversed[S ~[]T, T any](xs S) S

Reversed returns a new slice that is the reverse of the input slice.

func Shuffle

func Shuffle[S ~[]T, T any](xs S, prng io.Reader) (S, error)

Shuffle uses Fisher-Yates algorithm to produce a random permutation of the input. https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithmreally It ought not be called with relatively big len(xs) (e.g. that doesn't fit in 32 bits). Not only will it take a very long time, but with 2³¹! possible permutations, there's no way that any PRNG can have a big enough internal state to generate even a minuscule percentage of the possible permutations.

func Shuffled

func Shuffled[S ~[]T, T any](xs S, prng io.Reader) (S, error)

Shuffled returns a new slice that is a random permutation of the input slice.

Types

This section is empty.

Jump to

Keyboard shortcuts

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