Documentation ¶
Index ¶
- func Every[T any](vals []T, f func(T) bool) bool
- func Filter[T any](vals []T, f func(T) bool) []T
- func ForEach[T any](vals []T, f func(T))
- func Map[T any, V any](vals []T, f func(T) V) []V
- func MapConcurrent[T any, V any](vals []T, f func(T) V) []V
- func MapOrEmpty[T any](vals []Result[T]) [](T)
- func OnlyErr[T any, V any](f func(T) (V, error)) func(T) error
- type Mapper
- type MapperFn
- type Option
- type Result
- func Call[T any, V any](f func(T) (V, error), val T) Result[V]
- func Err[T any](err error) Result[T]
- func MapConcurrentResult[T any, V any](vals []T, f func(T) (V, error)) []Result[V]
- func MapResult[T any, V any](vals []T, f func(T) (V, error)) []Result[V]
- func NewResult[T any](val T, err error) Result[T]
- func Ok[T any](val T) Result[T]
- func Try[T any](f func() (T, error)) Result[T]
- func (r Result[T]) Error() error
- func (r Result[T]) ErrorIs(otherErr error) bool
- func (r Result[T]) Get() (T, error)
- func (r Result[T]) IfNotError(f func(T) error, ignore ...error) Result[T]
- func (r Result[T]) IsErr() bool
- func (r Result[T]) IsOk() bool
- func (r Result[T]) OrElse(val T) T
- func (r Result[T]) OrEmpty() T
- func (r Result[T]) ThenCall(f func(T) (T, error)) Result[T]
- func (r Result[T]) ThenCallResult(f func(T) Result[T]) Result[T]
- func (r Result[T]) ThenTry(f func() (T, error)) Result[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Every ¶
Every is a generic function that checks if all the elements in the input slice `vals` satisfy the provided condition `f`. The function `f` should return true if the element meets the condition, and false otherwise. It returns true if all elements satisfy the condition, and false otherwise.
func Filter ¶
Filter is a generic function that filters the input slice `vals` based on the provided filtering function `f`. The function `f` should return true for elements that need to be included in the filtered slice. It returns a new slice containing the filtered elements.
func ForEach ¶
func ForEach[T any](vals []T, f func(T))
ForEach runs thge given function for each element in the array
func Map ¶
Map is a generic function that maps the input slice `vals` to another type using the provided mapping function `f`. The function `f` should return a value of the desired output type for each input element. It returns a new slice containing the mapped elements.
func MapConcurrent ¶
MapConcurrent applies a function to each value in the input slice concurrently and returns a slice of the results. Note: the mapped result can be out of order of the original
func MapOrEmpty ¶
MapOrEmpty takes in a slice of Results of type T. It calls .OrEmpty on each element, and returns a slice of the resulting values.
Types ¶
type Result ¶
type Result[T any] struct { // contains filtered or unexported fields }
Result is a generic struct that holds a value or an error.x A result can be checked using the `IsOk()` and `IsErr()` functions. A result can be unwrapped with `Error()` and `Get()` functions.
func MapConcurrentResult ¶
MapConcurrentResult is similar to MapConcurrent but expects the function to return a value and an error. The results are returned as a slice of ConcurrentRunResult.
func MapResult ¶
MapResult is a generic function that maps the input slice `vals` to another type using the provided mapping function `f`. The function `f` should return a value of the desired output type for each input element, or an error. MapResult returns a new slice of Results of the given output type.
func Try ¶
Try runs the given function that possibly returns an error. It then returns a Result of the returned values
func (Result[T]) IfNotError ¶
IfNotError returns an error result if the previous result was an error. Otherwise, it returns a Result with the input value. IfNotError also takes in an optional array of errors to ignore.
func (Result[T]) OrElse ¶
func (r Result[T]) OrElse(val T) T
OrElse returns the Result value if the result is Ok, otherwise it will return the passed in value
func (Result[T]) OrEmpty ¶
func (r Result[T]) OrEmpty() T
OrElse returns the Result value if the result is Ok, otherwise it will return the default value for the given type
func (Result[T]) ThenCall ¶
ThenCall returns an error result if the previous result was an error. Otherwise, it returns the the output of the function as a Result.