generics

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2024 License: Apache-2.0 Imports: 3 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrDifferentLength = errors.New("arrays must be of equal length")
	ErrNotFound        = errors.New("not found")
)

Functions

func Apply

func Apply[A any](f func(A) error, arr []A) error

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 IsZeroValue[T any](a T) bool

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

func Map[A any, B any](f func(A) (B, error), arr []A) ([]B, error)

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 SafeApply added in v0.0.7

func SafeApply[A any](f func(A), arr []A)

func SafeMap added in v0.0.4

func SafeMap[A any, B any](f func(A) B, arr []A) []B

SafeMap applies a function to each element of an array

It returns an array of results

func SelectOne added in v0.0.4

func SelectOne[T any](arr []T, f func(T) bool) (T, error)

SelectOne returns the first element in an array that satisfies a predicate

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

type MapError struct {
	Errors map[int]error
}

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

func NewMapError() *MapError

NewMapError creates a new MapError

func (*MapError) Add added in v0.0.2

func (m *MapError) Add(idx int, err error)

Add adds an error to the MapError

func (*MapError) Error added in v0.0.2

func (m *MapError) Error() string

Error returns a string representation of the MapError

func (*MapError) HasError added in v0.0.2

func (m *MapError) HasError() bool

HasError returns true if the MapError has errors

type Pair

type Pair[A, B any] struct {
	A A
	B B
}

Pair is a tuple of two values

func Zip

func Zip[A any, B any](a []A, b []B) ([]Pair[A, B], error)

Zip combines two arrays into an array of pairs

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}]

Jump to

Keyboard shortcuts

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