sliceutil

package
v0.68.4 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 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.

Functions

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

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]
Map[T, U any](slice []T, transform func(T) U) []U

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"]
MapToSlice[K comparable, V any](m map[K]V) []K

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)
FilterMapKeys[K comparable, V any](m map[K]V, predicate func(K, V) bool) []K

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)
Any[T any](slice []T, predicate func(T) bool) bool

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 = true
Deduplicate[T comparable](slice []T) []T

Returns 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"]

Design Notes

  • Any is implemented via slices.ContainsFunc from the standard library.
  • Deduplicate uses a map[T]bool for O(n) time complexity.
  • None of these functions sort their output; callers that require sorted results should call slices.Sort on the returned slice.

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 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 MapToSlice

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

MapToSlice 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.

Types

This section is empty.

Jump to

Keyboard shortcuts

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