Documentation
¶
Overview ¶
Package generics provides generic utility functions for working with Go types.
This package implements common functional programming patterns like Map, Filter, and Reduce using Go's generics support. It aims to provide a simple, consistent API for working with collections in Go.
Index ¶
- Variables
- func Apply[A any](f func(A) error, arr []A) error
- func Compact[A comparable](arr []A) []A
- func Contains[T any](arr []T, f func(T) bool) bool
- func Filter[A any](arr []A, predicate func(A) bool) []A
- func ForEach[A any](arr []A, f func(A) error) error
- func IsZeroValue[T any](v T) bool
- func Map[A any, B any](f func(A) (B, error), arr []A) ([]B, error)
- func Reduce[A any, B any](arr []A, initial B, f func(B, A) B) B
- func SafeApply[A any](f func(A), arr []A)
- func SafeMap[A any, B any](f func(A) B, arr []A) []B
- func SelectOne[T any](arr []T, f func(T) bool) (T, error)
- type MapError
- type Pair
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrDifferentLength is returned when two slices of different lengths are provided // to a function that requires equal lengths. ErrDifferentLength = errors.New("arrays must be of equal length") // ErrNotFound is returned when an element matching a predicate is not found. ErrNotFound = errors.New("not found") )
Functions ¶
func Apply ¶
Apply applies a function to each element of a slice and collects all errors. It returns a MapError containing all non-nil errors returned by f, keyed by index. If all calls to f return nil, it returns nil.
Example ¶
arr := []int{1, 2, 3, 4, 5}
err := Apply(func(a int) error {
if a%2 == 0 {
return nil
}
return errors.New("testErr")
}, arr)
if err != nil {
fmt.Println(err)
}
Output: 3 errors
func Compact ¶ added in v0.0.3
func Compact[A comparable](arr []A) []A
Compact returns a new slice with all zero values removed.
Example ¶
arr := []int{1, 2, 3, 4, 5}
compacted := Compact(arr)
fmt.Println(compacted)
Output: [1 2 3 4 5]
func Contains ¶ added in v0.0.8
Contains returns true if the slice contains an element that satisfies the predicate.
Example ¶
arr := []int{1, 2, 3, 4, 5}
hasEven := Contains(arr, func(a int) bool {
return a%2 == 0
})
fmt.Println(hasEven)
Output: true
func Filter ¶ added in v0.0.8
Filter returns a new slice containing only the elements that satisfy the predicate. If the input slice is empty or nil, it returns the input slice.
Example ¶
arr := []int{1, 2, 3, 4, 5}
filtered := Filter(arr, func(a int) bool {
return a%2 == 0
})
fmt.Println(filtered)
Output: [2 4]
func ForEach ¶ added in v0.0.8
ForEach applies a function to each element of a slice and returns the first error encountered.
Unlike Apply, ForEach stops processing as soon as an error is encountered.
Example ¶
arr := []int{1, 2, 3, 4, 5}
sum := 0
err := ForEach(arr, func(a int) error {
sum += a
return nil
})
fmt.Println("Error:", err)
fmt.Println("Sum:", sum)
Output: Error: <nil> Sum: 15
func IsZeroValue ¶ added in v0.0.5
IsZeroValue returns true if the value v is the zero value of its type. It handles nil interfaces, pointers, slices, and maps, as well as primitive types and structs.
Example (Int) ¶
var a int fmt.Println(IsZeroValue(a)) a = 4 fmt.Println(IsZeroValue(a))
Output: true false
Example (Map) ¶
var a map[string]int
fmt.Println(IsZeroValue(a))
a = map[string]int{"a": 1, "b": 2}
fmt.Println(IsZeroValue(a))
Output: true false
Example (Slice) ¶
var a []int
fmt.Println(IsZeroValue(a))
a = []int{1, 2, 3}
fmt.Println(IsZeroValue(a))
Output: true false
Example (String) ¶
var a string fmt.Println(IsZeroValue(a)) a = "asdf" fmt.Println(IsZeroValue(a))
Output: true false
Example (Struct) ¶
type testStruct struct {
A int
B string
}
var b testStruct
fmt.Println(IsZeroValue(b))
b.A = 12
b.B = "34"
fmt.Println(IsZeroValue(b))
Output: true false
func Map ¶
Map applies a function that can return an error to each element of a slice. It returns a slice of all results and a MapError if any call to f returned an error.
Example ¶
arr := []int{1, 2, 3, 4, 5}
doubled, err := Map(func(a int) (int, error) {
return a * 2, nil
}, arr)
if err != nil {
fmt.Println(err)
}
fmt.Println(doubled)
Output: [2 4 6 8 10]
func Reduce ¶ added in v0.0.8
Reduce applies a function to each element in a slice, accumulating a single result. It starts with the initial value and sequentially applies f to the accumulator and each element.
Example ¶
arr := []int{1, 2, 3, 4, 5}
sum := Reduce(arr, 0, func(acc int, a int) int {
return acc + a
})
fmt.Println(sum)
Output: 15
func SafeApply ¶ added in v0.0.7
func SafeApply[A any](f func(A), arr []A)
SafeApply applies a function to each element of a slice. This is useful for side effects like logging or metrics collection.
func SafeMap ¶ added in v0.0.4
SafeMap applies a function to each element of a slice and returns a slice of the results.
func SelectOne ¶ added in v0.0.4
SelectOne returns the first element in a slice that satisfies the predicate. If no such element is found, it returns the zero value and ErrNotFound.
Example ¶
arr := []int{1, 2, 3, 4, 5}
selected, err := SelectOne(arr, func(a int) bool {
return a%2 == 0
})
if err != nil {
fmt.Println(err)
}
fmt.Println(selected)
Output: 2
Types ¶
type MapError ¶ added in v0.0.2
MapError is a collection of errors returned by functions applied to slices. It maps the index of the element that caused the error to the error itself.
func NewMapError ¶ added in v0.0.2
func NewMapError() *MapError
NewMapError creates a new, empty MapError.
type Pair ¶
type Pair[A, B any] struct { A A B B }
Pair is a simple tuple of two values of possibly different types.
func Zip ¶
Zip combines two slices into a single slice of Pairs. It returns ErrDifferentLength if the slices are not the same length.
Example ¶
arr1 := []int{1, 2, 3, 4, 5}
arr2 := []int{5, 4, 3, 2, 1}
zipped, err := Zip(arr1, arr2)
if err != nil {
fmt.Println(err)
}
fmt.Println(zipped)
Output: [{1 5} {2 4} {3 3} {4 2} {5 1}]