uarray

package
v3.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

uarray

Slice utilities for searching, filtering, mapping, grouping, set-like operations, safe indexing, chunking, and string conversion.

Use this package when the standard slices package is too low-level for common collection workflows.

Example

values := []int{1, 2, 3, 4}
even := uarray.Filter(values, func(v int) bool { return v%2 == 0 })
sum := uarray.Reduce(values, 0, func(acc, v int) int { return acc + v })

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllMatch

func AllMatch[T any](values []T, predicate func(v T) bool) bool

AllMatch checks if all slice elements match the predicate. Returns true if all elements match the predicate, false otherwise.

func AnyMatch

func AnyMatch[T any](values []T, predicate func(v T) bool) bool

AnyMatch checks if slice has an element that matches predicate. Returns true if there's a match, false otherwise.

func AsString

func AsString[T uconst.Stringable](delimiter string, values ...T) string

AsString converts any supported stringable value to a string and joins them with the specified delimiter.

func At

func At[T any](values []T, index int) *T

At returns a pointer to the element at index, or nil when index is out of range.

func AtOr

func AtOr[T any](values []T, index int, fallback T) T

AtOr returns the element at index, or fallback when index is out of range.

func BestMatchBy

func BestMatchBy[T any](values []T, predicate func(currentBest, candidate T) bool) *T

BestMatchBy iterates over the slice and selects the element that satisfies the predicate function as the best match. The predicate function compares the current best match with the candidate and determines if the candidate is better. Returns a pointer to the selected element or nil if the slice is empty.

func ClampIndex

func ClampIndex[T any](values []T, index int) int

ClampIndex clamps index to the valid range of values. It returns -1 for empty slices.

func CollectAsMap

func CollectAsMap[K comparable, V, R any](values []V, key func(v V) K, val func(v V) R) map[K]R

CollectAsMap collects corresponding values to a map.

func Compact

func Compact[T comparable](values []T) []T

Compact returns a copy without zero-value elements.

func CompactFunc

func CompactFunc[T any](values []T, empty func(v T) bool) []T

CompactFunc returns a copy without elements that match empty predicate.

func Contains

func Contains[V comparable](values []V, val V) int

Contains checks if slice contains specified element. Returns its index if found, -1 otherwise.

func ContainsAny

func ContainsAny[V comparable](from []V, values ...V) int

ContainsAny checks if slice contains any element from another slice. Returns its index if found, -1 otherwise.

func ContainsPredicate

func ContainsPredicate[T any](values []T, predicate func(v T) bool) (int, *T)

ContainsPredicate checks if slice contains specified struct element by using a predicate. Returns its index and value if found, -1 and nil otherwise.

func ContainsStruct

func ContainsStruct[K comparable, V Indexed[K]](values []V, val V) (int, *V)

ContainsStruct checks if slice contains specified struct element. Returns its index and value if found, -1 and nil otherwise.

func CopyWithoutIndex

func CopyWithoutIndex[T any](src []T, index int) []T

CopyWithoutIndex copies a slice while ignoring an element at specific index.

func CopyWithoutIndexes

func CopyWithoutIndexes[T any](src []T, indexes []int) []T

CopyWithoutIndexes copies a slice while ignoring elements at specific indexes. Duplicate values for indexes are ignored.

func CountBy

func CountBy[T any, K comparable](values []T, key func(v T) K) map[K]int

CountBy counts values grouped by key.

func Difference

func Difference[T comparable](left, right []T) []T

Difference returns a copy of left values that are not present in right. The order and duplicate values from left are preserved.

func EqualValues

func EqualValues[T cmp.Ordered](left []T, right []T) bool

EqualValues compares values of two slices regardless of elements order.

func EqualValuesCompare

func EqualValuesCompare[T any](left []T, right []T, compare func(t1, t2 T) bool, less func(t1, t2 T) bool) bool

EqualValuesCompare compares values of two slices regardless of elements order.

func EqualsCompareWithOrder

func EqualsCompareWithOrder[T any](left []T, right []T, compare func(t1 T, t2 T) bool) bool

EqualsCompareWithOrder compares two slices taking into consideration elements order.

func EqualsWithOrder

func EqualsWithOrder[T comparable](left []T, right []T) bool

EqualsWithOrder compares two slices taking into consideration elements order.

func Filter

func Filter[V any](values []V, filter func(v V) bool) []V

Filter filters values slice and returns a copy with filtered elements matching a predicate.

func FilterAll

func FilterAll[V any](values []V, filter func(v V) bool) ([]V, []V)

FilterAll filters values slice and returns a copy with filtered elements matching a predicate and elements that do not match any filter.

func FilterBySet

func FilterBySet[V comparable](values []V, filter ...V) []V

FilterBySet filters values slice and returns a copy with filtered elements matching values from filter.

func FilterOut

func FilterOut[V any](values []V, filter func(v V) bool) []V

FilterOut is a macros to Filter, so it acts like Filter, but filters out values. That means that only values not matching the filter will be returned.

func FilterOutBySet

func FilterOutBySet[V comparable](values []V, filter ...V) []V

FilterOutBySet filters out values slice and returns a copy without filtered elements matching values from filter.

func Find

func Find[V any](values []V, filter func(v V) bool) *V

Find finds the first match in a slice using simple looping.

func FindBinary

func FindBinary[V any](values []V, filter func(v V) bool) *V

FindBinary finds the first match in a sorted slice using binary search. The filter function should implement a comparison suitable for binary search.

func FindIndex

func FindIndex[T any](values []T, predicate func(v T) bool) int

FindIndex returns the index of the first element matching predicate, or -1 when no element matches.

func Flat

func Flat[V any](values [][]V) []V

Flat flattens the stream (slice).

func FlatMap

func FlatMap[V, R any](values [][]V, m func(v V) R) []R

FlatMap applies the Map method and the Flat method consequently.

func GroupBy

func GroupBy[V any, G comparable](values []V, group func(v V) G, aggregator func(v1, v2 V) V) []V

GroupBy groups and aggregates elements with aggregator method func.

func GroupToMapBy

func GroupToMapBy[V any, G comparable](values []V, group func(v V) G) map[G][]V

GroupToMapBy groups elements with group method func.

func Has

func Has[T comparable](values []T, val T) bool

Has checks if slice has an element. Returns true if there's a match, false otherwise.

func IndexBy

func IndexBy[T any, K comparable](values []T, key func(v T) K) map[K]T

IndexBy indexes values by key. If multiple values produce the same key, the last value wins.

func IndexOfUint32

func IndexOfUint32(slice []uint32, value uint32) int

func Intersect

func Intersect[T comparable](left, right []T) []T

Intersect returns a copy of left values that are present in right. The order and duplicate values from left are preserved.

func Map

func Map[V, R any](values []V, m func(v V) R) []R

Map maps a func and returns a result.

func MapAggr

func MapAggr[V, R any](values []V, aggr func(v V) []R) []R

MapAggr maps a func to each set of elements and returns an aggregated result.

func MapAndGroupToMapBy

func MapAndGroupToMapBy[V any, G comparable, R any](values []V, group func(v V) (G, R)) map[G][]R

MapAndGroupToMapBy same as GroupToMapBy, but allows elements to be mapped to a different type.

func Merge

func Merge[K comparable, T any](t1 []T, t2 []T, key func(t T) K) []T

Merge merges two slices with t1 elements prioritized against elements of t2.

func Range

func Range[T uconst.Integer](from, to T) []T

Range generates a slice of integers from 'from' to 'to' (exclusive). The type T must be an integer type (e.g., int, int64, uint, etc.). The returned slice includes 'from', but is exclusive to 'to'. Example usage: FromRange(1, 5) returns []int{1, 2, 3, 4}.

func RangeWithStep

func RangeWithStep(from, to, step int) []int

RangeWithStep generates a slice of integers starting from 'from' up to and including 'to' with a specified step.

func Reduce

func Reduce[T, R any](values []T, initial R, reducer func(acc R, v T) R) R

Reduce folds values into a single result by applying reducer from left to right.

func Reverse

func Reverse[T any](values []T) []T

Reverse returns a reversed copy of values.

func ReverseInPlace

func ReverseInPlace[T any](values []T)

ReverseInPlace reverses values in place.

func SortFind

func SortFind[V any](values []V, less func(a, b V) bool, filter func(V) bool) *V

SortFind sorts the given slice using the provided less function and then finds the first match using a binary search with the filter function. This approach is efficient for large slices and repeated searches, as it leverages the speed of binary search. NOTE: please note that the original slice will be modified.

Parameters:

  • values: the slice of elements to search through.
  • less: a function that defines the order of elements for sorting.
  • filter: a function that tests each element to find a match.

Returns:

  • A pointer to the found element, or nil if no match is found.

func Split

func Split[T any](slice []T, chunkSize int) [][]T

Split divides a slice into multiple smaller slices (chunks) of a specified size and returns a slice of these chunks.

If chunkSize is less than or equal to zero, the function returns a slice containing the original slice as its only element. If the length of the input slice isn't perfectly divisible by chunkSize, the last chunk will contain the remaining elements.

Example:

input:    []int{1, 2, 3, 4, 5}, chunkSize: 2
output:   [][]int{{1, 2}, {3, 4}, {5}}

Type Parameters:

T - The type of elements in the slice.

Parameters:

slice - The input slice to be split.
chunkSize - The desired size of each chunk.

Returns:

A two-dimensional slice where each inner slice is a chunk of the original slice.

func ToMap

func ToMap[V any, K comparable, R any](values []V, m func(v V) (K, R)) map[K]R

ToMap collects elements of a slice into a map using a collector function.

If the mapping function produces the same key for multiple elements, the resulting map will contain only the last value associated with that key, as the map does not behave like a multimap. Each key in the returned map corresponds to a single value, and any previous value for the same key will be overwritten.

func ToMultiMap

func ToMultiMap[V any, K comparable, R any](values []V, m func(v V) (K, R)) map[K][]R

ToMultiMap collects a stream using collector func to a multimap.

func Uniq

func Uniq[V any, F comparable](values []V, getter func(v V) F) []V

Uniq filters unique elements by predicate that returns any comparable value.

func Unique

func Unique[V comparable](values []V, transform ...func(v V) V) []V

Unique filters unique elements from a slice.

Types

type Indexed

type Indexed[T any] interface {
	GetIndex() T
}

Jump to

Keyboard shortcuts

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