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 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 ¶
View Source
var (
ErrDifferentLength = errors.New("arrays must be of equal length")
)
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
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]
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
func NewMapError ¶ added in v0.0.2
NewMapError creates a new MapError
Click to show internal directories.
Click to hide internal directories.