The sliceutil package provides generic utility functions for working with slices and maps.
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.
Returns a new slice containing only elements for which predicate returns true.
import "github.com/github/gh-aw/pkg/sliceutil"
numbers := []int{1, 2, 3, 4, 5}
evens := sliceutil.Filter(numbers, func(n int) bool { return n%2 == 0 })
// evens = [2, 4]Applies transform to every element and returns the results as a new slice.
names := []string{"alice", "bob"}
upper := sliceutil.Map(names, strings.ToUpper)
// upper = ["ALICE", "BOB"]Converts the keys of a map into a slice. Order is not guaranteed.
m := map[string]int{"a": 1, "b": 2}
keys := sliceutil.MapToSlice(m)
// keys = ["a", "b"] (in some order)Returns the map keys for which predicate(key, value) is true. Order is not guaranteed.
scores := map[string]int{"alice": 90, "bob": 50, "carol": 80}
passed := sliceutil.FilterMapKeys(scores, func(name string, score int) bool {
return score >= 75
})
// passed = ["alice", "carol"] (in some order)Returns true if at least one element in slice satisfies predicate. Returns false for nil or empty slices.
words := []string{"hello", "world"}
hasWorld := sliceutil.Any(words, func(w string) bool { return w == "world" })
// hasWorld = trueReturns a new slice with duplicate elements removed, preserving the order of first occurrence.
items := []string{"a", "b", "a", "c", "b"}
unique := sliceutil.Deduplicate(items)
// unique = ["a", "b", "c"]Anyis implemented viaslices.ContainsFuncfrom the standard library.Deduplicateuses amap[T]boolfor O(n) time complexity.- None of these functions sort their output; callers that require sorted results should call
slices.Sorton the returned slice.
This specification is automatically maintained by the spec-extractor workflow.