Documentation
¶
Overview ¶
Package f is a golang library implementing some basic algorithms.
The canonical import for this library is go.bug.st/f:
import "go.bug.st/f"
Index ¶
- func Assert(condition bool, msg string, args ...any)
- func DiscardCh[T any](ch <-chan T)
- func Filter[T any](values []T, matcher Matcher[T]) []T
- func Map[T, U any](values []T, mapper Mapper[T, U]) []U
- func Must[T any](val T, err error) T
- func ParallelMap[T, U any](values []T, mapper Mapper[T, U], jobs ...int) []U
- func Ptr[T any](v T) *T
- func Reduce[T any](values []T, reducer Reducer[T], initialValue ...T) T
- func Uniq[T comparable](in []T) []T
- func UnwrapOrDefault[T any](ptr *T) T
- type Future
- type Mapper
- type Matcher
- type Reducer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DiscardCh ¶
func DiscardCh[T any](ch <-chan T)
DiscardCh consumes all incoming messages from the given channel until it's closed.
func Filter ¶
Filter takes a slice of type []T and a Matcher[T]. It returns a newly allocated slice containing only those elements of the input slice that satisfy the matcher.
func Map ¶
Map applies the Mapper function to each element of the slice and returns a new slice with the results in the same order.
func Must ¶
Must should be used to wrap a call to a function returning a value and an error. Must returns the value if the errors is nil, or panics otherwise.
func ParallelMap ¶
ParallelMap applies the Mapper function to each element of the slice and returns a new slice with the results in the same order. This is executed among multilple goroutines in parallel. If jobs is specified it will indicate the maximum number of goroutines to be spawned.
func Reduce ¶
Reduce applies the Reducer function to all elements of the input values and returns the result.
func Uniq ¶
func Uniq[T comparable](in []T) []T
Uniq return a copy of the input array with all duplicates removed
func UnwrapOrDefault ¶ added in v0.3.0
func UnwrapOrDefault[T any](ptr *T) T
UnwrapOrDefault returns the ptr value if it is not nil, otherwise returns the zero value.
Types ¶
type Future ¶
type Future[T any] struct { // contains filtered or unexported fields }
Future is an object that holds a result value. The value may be read and written asynchronously.
type Mapper ¶
type Mapper[T, U any] func(T) U
Mapper is a function that converts a value of one type to another type.
type Matcher ¶
Matcher is a function that tests if a given value matches a certain criteria.
func Equals ¶
func Equals[T comparable](value T) Matcher[T]
Equals return a Matcher that matches the given value
func NotEquals ¶
func NotEquals[T comparable](value T) Matcher[T]
NotEquals return a Matcher that does not match the given value