Documentation
¶
Overview ¶
Package slices provides generic utility functions for working with slices. These functions are designed to be simple, efficient, and type-safe using Go generics.
Index ¶
- func Contains[T comparable](slice []T, element T) bool
- func ContainsAll[T comparable](slice []T, elements []T) bool
- func ContainsAny[T comparable](slice []T, elements []T) bool
- func Copy[T any](slice []T) []T
- func Filter[T any](slice []T, predicate func(T) bool) []T
- func First[T any](slice []T) (T, bool)
- func IndexOf[T comparable](slice []T, element T) int
- func Last[T any](slice []T) (T, bool)
- func Map[T any, R any](slice []T, transform func(T) R) []R
- func Unique[T comparable](slice []T) []T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable](slice []T, element T) bool
Contains returns true if the slice contains the given element. Uses comparable constraint for type-safe equality comparison.
Example ¶
package main
import (
"fmt"
"github.com/andygeiss/cloud-native-utils/slices"
)
func main() {
fmt.Println(slices.Contains([]int{1, 2, 3}, 2))
fmt.Println(slices.Contains([]int{1, 2, 3}, 9))
}
Output: true false
func ContainsAll ¶
func ContainsAll[T comparable](slice []T, elements []T) bool
ContainsAll returns true if the slice contains all of the given elements.
func ContainsAny ¶
func ContainsAny[T comparable](slice []T, elements []T) bool
ContainsAny returns true if the slice contains any of the given elements.
func Filter ¶
Filter returns a new slice containing only elements that satisfy the predicate.
Example ¶
package main
import (
"fmt"
"github.com/andygeiss/cloud-native-utils/slices"
)
func main() {
even := slices.Filter([]int{1, 2, 3, 4, 5, 6}, func(n int) bool { return n%2 == 0 })
fmt.Println(even)
}
Output: [2 4 6]
func First ¶
First returns the first element of the slice and true, or zero value and false if empty.
Example ¶
package main
import (
"fmt"
"github.com/andygeiss/cloud-native-utils/slices"
)
func main() {
value, ok := slices.First([]string{"alpha", "beta"})
fmt.Println(value, ok)
// An empty slice reports false rather than panicking.
_, ok = slices.First([]string{})
fmt.Println(ok)
}
Output: alpha true false
func IndexOf ¶
func IndexOf[T comparable](slice []T, element T) int
IndexOf returns the index of the first occurrence of element in the slice, or -1 if the element is not found.
func Map ¶
Map transforms each element in the slice using the given function.
Example ¶
package main
import (
"fmt"
"github.com/andygeiss/cloud-native-utils/slices"
)
func main() {
lengths := slices.Map([]string{"go", "rust", "zig"}, func(s string) int { return len(s) })
fmt.Println(lengths)
}
Output: [2 4 3]
func Unique ¶
func Unique[T comparable](slice []T) []T
Unique returns a new slice with duplicate elements removed. Preserves the order of first occurrence.
Example ¶
package main
import (
"fmt"
"github.com/andygeiss/cloud-native-utils/slices"
)
func main() {
fmt.Println(slices.Unique([]string{"a", "b", "a", "c", "b"}))
}
Output: [a b c]
Types ¶
This section is empty.