Documentation
¶
Index ¶
- Variables
- func Apply[A any](f func(A) error, arr []A) error
- func Compact[A comparable](arr []A) []A
- func IsZeroValue[T any](a T) bool
- func Map[A any, B any](f func(A) (B, error), arr []A) ([]B, error)
- 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 = errors.New("arrays must be of equal length") ErrNotFound = errors.New("not found") )
Functions ¶
func Apply ¶
Apply applies a function to each element of an array
It returns an array of errors, where each error corresponds to the result of the function applied to the element at the same index ¶
It returns nil if all function calls return 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 removes zero values from an array
Example ¶
arr := []int{1, 2, 3, 4, 5}
compacted := Compact(arr)
fmt.Println(compacted)
Output: [1 2 3 4 5]
func IsZeroValue ¶ added in v0.0.5
IsZeroValue returns true if the value is the zero value of its type.
Example (Int) ¶
var a int fmt.Println(IsZeroValue(a)) a = 4 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 to each element of an array
It returns an array of results and an array of errors, where each result and error corresponds to the result of the function applied to the element at the same index
It returns nil if all function calls return nil
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 SafeMap ¶ added in v0.0.4
SafeMap applies a function to each element of an array
It returns an array of results
Types ¶
type MapError ¶ added in v0.0.2
MapError is a collection of errors
It is used to collect errors from a function applied to each element of an array
type Pair ¶
type Pair[A, B any] struct { A A B B }
Pair is a tuple of two values