Documentation
¶
Overview ¶
Package fun is a zero-dependency collection of tools and idoms that takes advantage of generics. Iterators, error handling, a native-feeling Set type, and a simple pub-sub framework for distributing messages in fan-out patterns.
Index ¶
- Variables
- func Check(fn func()) (err error)
- func Invariant(cond bool, args ...any)
- func InvariantCheck(fn func() error, args ...any)
- func InvariantMust(err error, args ...any)
- func Is[T any](in any) bool
- func IsInvariantViolation(r any) bool
- func Must[T any](arg T, err error) T
- func Safe[T any](fn func() T) (out T, err error)
- func SafeCtx[T any](ctx context.Context, fn func(context.Context) T) (out T, err error)
- func Unwrap[T any](in T) T
- func Wait(ctx context.Context, wg *sync.WaitGroup)
- func WaitAdd(ctx context.Context, wg *sync.WaitGroup, fn WaitFunc)
- type Iterator
- type WaitFunc
- func WaitChannel[T any](ch <-chan T) WaitFunc
- func WaitContext(ctx context.Context) WaitFunc
- func WaitGroup(wg *sync.WaitGroup) WaitFunc
- func WaitMerge(ctx context.Context, iter Iterator[WaitFunc]) WaitFunc
- func WaitObserve[T any](observe func(T), ch <-chan T) WaitFunc
- func WaitObserveAll[T any](observe func(T), ch <-chan T) WaitFunc
Constants ¶
This section is empty.
Variables ¶
var ErrInvariantViolation = errors.New("invariant violation")
ErrInvariantViolation is the root error of the error object that is the content of all panics produced by the Invariant helper.
Functions ¶
func Check ¶ added in v0.3.0
func Check(fn func()) (err error)
Check, like safe and SafeCtx runs a function without arguments that does not produce an error, and, if the function panics, converts it into an error.
func Invariant ¶ added in v0.3.0
Invariant panics if the condition is false Invariant panics, passing an error that is rooted by ErrInvariantViolation.
func InvariantCheck ¶ added in v0.6.0
InvariantCheck calls the function and if it returns an error panics with an ErrInvariantViolation error, wrapped with the error of the function, and any annotation arguments.
func InvariantMust ¶ added in v0.6.0
InvariantMust raises an invariant error if the error is not nil. The content of the panic is both--via wrapping--an ErrInvariantViolation and the error itself.
func Is ¶
Is a generic version of `errors.Is` that takes advantage of the Unwrap function, and is useful for checking if an object of an interface type is or wraps an implementation of the type parameter.
func IsInvariantViolation ¶ added in v0.3.0
IsInvariantViolation returns true if the argument is or resolves to ErrInvariantViolation.
func Must ¶
Must wraps a function that returns a value and an error, and converts the error to a panic.
func Unwrap ¶
func Unwrap[T any](in T) T
Unwrap is a generic equivalent of the `errors.Unwrap()` function for any type that implements an `Unwrap() T` method. useful in combination with Is.
Types ¶
type Iterator ¶
Iterator provides a safe, context-respecting iterator paradigm for iterable objects, along with a set of consumer functions and basic implementations.
The itertool package provides a number of tools and paradigms for creating and processing Iterator objects, including Generators, Map and Reduce, Filter as well as Split and Merge to combine or divide iterators.
In general, Iterators cannot be safe for access from multiple concurrent goroutines, because it is impossible to synchronize calls to Next() and Value(); however, itertool.Range() and itertool.Split() provide support for these workloads.
type WaitFunc ¶ added in v0.6.3
WaitFunc is a type of function object that will block until an operation returns or the context is canceled.
func WaitChannel ¶ added in v0.6.3
WaitChannel converts a channel (typically, a `chan struct{}`) to a
func WaitContext ¶ added in v0.6.3
WaitContext wait's for the context to be canceled before returning. The WaitFunc that's return also respects it's own context. Use this WaitFunc and it's own context to wait for a context to be cacneled with a timeout, for instance.
func WaitGroup ¶ added in v0.6.3
WaitGroup converts a WaitGroup into a fun.WaitFunc.
This operation will leak a go routine if the WaitGroup never returns and the context is canceled.
func WaitMerge ¶ added in v0.6.3
WaitMerge starts a goroutine that blocks on each WaitFunc provided and returns a WaitFunc that waits for all of these goroutines to return. The constituent WaitFunc are passed WaitMerge's context, while the returned WaitFunc respects its own context.
User itertool.Variadic, itertool.Slice, or itertool.Channel to convert common container types/calling patterns to an iterator.
func WaitObserve ¶ added in v0.6.3
WaitObserve passes the output of the channel into the observer function and then returns. If the context is canceled the output of the channel is not observed.
WaitObserve consumes and observes, at most, one item from the channel. Callers must call the WaitFunc.
func WaitObserveAll ¶ added in v0.6.3
WaitObserveAll passes the output of the channel into the observer function, waiting for the input channel to be closed or the WaitFunc's context to be canceled. WaitObserveAll does not begin processing the channel until the WaitFunc is called.
Directories
¶
Path | Synopsis |
---|---|
Package erc provides a simple/fast error aggregation tool for collecting and aggregating errors.
|
Package erc provides a simple/fast error aggregation tool for collecting and aggregating errors. |
Package itertool provides a set of functional helpers for managinging and using fun.Iterator implementations, including a parallel Map/Reduce, Merge, and other convenient tools.
|
Package itertool provides a set of functional helpers for managinging and using fun.Iterator implementations, including a parallel Map/Reduce, Merge, and other convenient tools. |
Package pubsub provides a message broker for one-to-many or many-to-many message distribution.
|
Package pubsub provides a message broker for one-to-many or many-to-many message distribution. |
Package seq provides single and double linked-list implementations and tools (e.g.
|
Package seq provides single and double linked-list implementations and tools (e.g. |
Package Set provides ordered and unordered set implementations for arbitrary comparable types.
|
Package Set provides ordered and unordered set implementations for arbitrary comparable types. |
Package srv provides a framework and toolkit for service orchestration.
|
Package srv provides a framework and toolkit for service orchestration. |