sliceutil

package
v0.75.3 Latest Latest
Warning

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

Go to latest
Published: May 23, 2026 License: MIT Imports: 1 Imported by: 0

README

sliceutil Package

The sliceutil package provides generic utility functions for working with slices and maps.

Overview

All functions in this package are pure: they never modify their input. They are generic and work with any element type using Go's type-parameter syntax.

Public API

Functions
Function Signature Description
Filter func[T any](slice []T, predicate func(T) bool) []T Returns a new slice containing only elements for which predicate returns true
Map func[T, U any](slice []T, transform func(T) U) []U Applies transform to every element and returns the results as a new slice
MapKeys func[K comparable, V any](m map[K]V) []K Converts the keys of a map into a slice; order is not guaranteed
FilterMapKeys func[K comparable, V any](m map[K]V, predicate func(K, V) bool) []K Returns map keys for which predicate(key, value) is true; order is not guaranteed
Any func[T any](slice []T, predicate func(T) bool) bool Returns true if at least one element satisfies predicate; returns false for nil or empty slices
Deduplicate func[T comparable](slice []T) []T Returns a new slice with duplicate elements removed, preserving order of first occurrence
MergeUnique func[T comparable](base []T, extra ...T) []T Returns a deduplicated slice starting with base and appending unseen values from extra
Exclude func[T comparable](base []T, exclude ...T) []T Returns a new slice with all exclude values removed while preserving order

Usage Examples

import "github.com/github/gh-aw/pkg/sliceutil"

// Filter a slice
numbers := []int{1, 2, 3, 4, 5}
evens := sliceutil.Filter(numbers, func(n int) bool { return n%2 == 0 })
// evens = [2, 4]

// Map a slice
names := []string{"alice", "bob"}
upper := sliceutil.Map(names, strings.ToUpper)
// upper = ["ALICE", "BOB"]

// Deduplicate
items := []string{"a", "b", "a", "c"}
unique := sliceutil.Deduplicate(items)
// unique = ["a", "b", "c"]

// Merge unique values
merged := sliceutil.MergeUnique([]string{"a", "b"}, "b", "c")
// merged = ["a", "b", "c"]

// Exclude values
filtered := sliceutil.Exclude([]string{"a", "b", "c"}, "b")
// filtered = ["a", "c"]

Design Notes

  • Any is implemented via slices.ContainsFunc from the standard library.
  • Deduplicate, MergeUnique, and Exclude use hash sets (map[T]struct{}) for O(n) behavior.
  • None of these functions sort their output; callers that require sorted results should call slices.Sort on the returned slice.

This specification is automatically maintained by the spec-extractor workflow.

Documentation

Overview

Package sliceutil provides utility functions for working with slices.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any added in v0.58.0

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

Any returns true if at least one element in the slice satisfies the predicate. Returns false for nil or empty slices. This is a pure function that does not modify the input slice.

func Deduplicate

func Deduplicate[T comparable](slice []T) []T

Deduplicate returns a new slice with duplicate elements removed. The order of first occurrence is preserved. This is a pure function that does not modify the input slice.

func Exclude added in v0.72.2

func Exclude[T comparable](base []T, exclude ...T) []T

Exclude returns a new slice containing the items from base that do not appear in the exclude set. Order of remaining items is preserved. Always returns a fresh slice (never aliases base) even when no items are removed.

func Filter

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

Filter returns a new slice containing only elements that match the predicate. This is a pure function that does not modify the input slice.

func FilterMapKeys

func FilterMapKeys[K comparable, V any](m map[K]V, predicate func(K, V) bool) []K

FilterMapKeys returns map keys that match the given predicate. The order of elements is not guaranteed as map iteration order is undefined. This is a pure function that does not modify the input map.

func Map

func Map[T, U any](slice []T, transform func(T) U) []U

Map transforms each element in a slice using the provided function. This is a pure function that does not modify the input slice.

func MapKeys added in v0.74.1

func MapKeys[K comparable, V any](m map[K]V) []K

MapKeys converts a map's keys to a slice. The order of elements is not guaranteed as map iteration order is undefined. This is a pure function that does not modify the input map.

func MergeUnique added in v0.72.2

func MergeUnique[T comparable](base []T, extra ...T) []T

MergeUnique returns a deduplicated slice that starts with base and appends any items from extra that are not already present in base. Order is preserved.

Types

This section is empty.

Jump to

Keyboard shortcuts

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