Documentation
¶
Index ¶
- func Apply[T any](fn func(T), arg T) func()
- func Cast[T any](in any) (v T, ok bool)
- func Contains[T comparable](item T, slice []T) booldeprecated
- func Default[T comparable](input T, defaultValue T) T
- func DefaultFuture[T comparable](input T, fn func() T) T
- func DefaultNew[T any](input *T) *T
- func DoTimes(n int, op func())
- func Flip[A any, B any](first A, second B) (B, A)
- func IfCall(cond bool, doIf func(), doElse func())
- func IfDo[T any](cond bool, doIf func() T, doElse func() T) T
- func IfValue[T any](cond bool, ifVal T, elseVal T) T
- func Ignore[T any](_ T)
- func IgnoreFirst[A any, B any](_ A, b B) B
- func IgnoreSecond[A any, B any](a A, _ B) A
- func IsNil(in any) bool
- func IsOk[T any](_ T, ok bool) bool
- func IsPtr(in any) bool
- func IsType[T any](in any) bool
- func IsZero[T comparable](in T) bool
- func Join(fns ...func()) func()
- func Must[T any](arg T, err error) T
- func MustBeOk[T any](out T, ok bool) T
- func Not(p bool) bool
- func NotZero[T comparable](in T) bool
- func Once(f func()) func()
- func OnceDo[T any](op func() T) func() T
- func Ptr[T any](in T) *T
- func Ref[T any](in *T) T
- func RefOk[T any](in *T) (value T, ok bool)
- func SafeCall(op func())
- func SafeCast[T any](in any) T
- func SafeDo[T any](op func() T) (out T)
- func SafeWrap(op func()) func()
- func UnlessCall(cond bool, op func())
- func UnlessDo[T any](cond bool, op func() T) T
- func WhenApply[T any](cond bool, op func(T), arg T)
- func WhenApplyFuture[T any](cond bool, op func(T), arg func() T)
- func WhenCall(cond bool, op func())
- func WhenDo[T any](cond bool, op func() T) (out T)
- func WhenHandle[T any](cond func(T) bool, op func(T), in T)
- func WithContext(op func(context.Context))
- func WithTimeout(dur time.Duration, op func(context.Context))
- func Wrapper[T any](in T) func() T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶ added in v0.10.3
func Apply[T any](fn func(T), arg T) func()
Apply returns a function object that, when called calls the input function with the provided argument.
func Contains
deprecated
func Contains[T comparable](item T, slice []T) bool
Contain returns true if an element of the slice is equal to the item.
Deprecated: use slices.Contains from the standard library.
func Default ¶
func Default[T comparable](input T, defaultValue T) T
Default takes two values. if the first value is the zero value for the type T, then Default returns the second (default) value. Otherwise it returns the first input type.
func DefaultFuture ¶ added in v0.10.5
func DefaultFuture[T comparable](input T, fn func() T) T
DefaultFuture combines Default() and WhenDo: if the input value is NOT the zero value for the comparable type T it is returned directly; otherwise DefaultFuture calls the provided function and returns its output.
func DefaultNew ¶ added in v0.10.4
func DefaultNew[T any](input *T) *T
DefaultNew checks a pointer to a value, and when it is nil, constructs a new value of the same type.
func Flip ¶
Flip takes two arguments and returns them in the opposite order. Intended to wrap other functions to reduce the friction when briding APIs.
func IfCall ¶ added in v0.10.4
func IfCall(cond bool, doIf func(), doElse func())
IfCall is, effectively the if-form from (many) lisps: when the condition is true, the first function is called, and otherwise the second. If the appropriate function is nil, IfCall is a noop. The "other" function call is never called.
func IfDo ¶ added in v0.10.4
IfDo returns the output of the first function when the condition is false, and the value of the second function otherwise. If the appropriate function is nil, IfDo returns the zero value for the type. The "other" function call is never called.
func IfValue ¶ added in v0.10.5
IfValue provides a ternary-like operation as a complement to IfDo and IfCall for values.
func Ignore ¶ added in v0.10.3
func Ignore[T any](_ T)
Ignore is a noop, but can be used to annotate operations rather than assigning to the empty identifier:
_ = operation() ft.Ignore(operation())
func IgnoreFirst ¶
IgnoreFirst takes two arguments and returns only the second, for use in wrapping functions that return two values.
func IgnoreSecond ¶
IgnoreSecond takes two arguments and returns only the first, for use when wrapping functions that return two values.
func IsOk ¶ added in v0.10.9
IsOk returns only the second argument passed to it, given a function that returns two values where the second value is a boolean, you can use IsOk to discard the first value.
func IsZero ¶
func IsZero[T comparable](in T) bool
IsZero returns true if the input value compares "true" to the zero value for the type of the argument. If the type implements an IsZero() method (e.g. time.Time), then IsZero returns that value, otherwise, IsZero constructs a zero valued object of type T and compares the input value to the zero value.
func Join ¶ added in v0.10.8
func Join(fns ...func()) func()
Join creates a function that iterates over all of the input functions and calls all non-nil functions sequentially. Nil functions are ignored.
func Must ¶
Must wraps a function that returns a value and an error, and converts the error to a panic.
func MustBeOk ¶
MustBeOk raises an invariant violation if the ok value is false, and returns the first value if the second value is ok. Useful as in:
out := ft.MustBeOk(func() (string, bool) { return "hello world", true })
func NotZero ¶
func NotZero[T comparable](in T) bool
NotZero returns true when the item does not have the zero value for the type T.
func Once ¶
func Once(f func()) func()
Once uses a sync.Once to wrap to provide an output function that will execute at most one time, while eliding/managing the sync.Once object.
func OnceDo ¶
func OnceDo[T any](op func() T) func() T
OnceDo returns a function, that will run exactly once. The value returned by the inner function is cached transparently, so subsequent calls to the function returned by OnceDo will return the original value.
func Ptr ¶
func Ptr[T any](in T) *T
Ptr returns a pointer for the object. Useful for setting the value in structs where you cannot easily create a reference (e.g. the output of functions, and for constant literals.). If you pass a value that is a pointer (e.x. *string), then Ptr returns **string. If the input object is a nil pointer, then Ptr returns a non-nil pointer to a nil pointer.
func Ref ¶
func Ref[T any](in *T) T
Ref takes a pointer to an value and dereferences it, If the input value is nil, the output value is the zero value of the type.
func RefOk ¶ added in v0.10.9
RefOk takes a pointer to an value and returns the concrete type for that pointer. If the pointer is nil, RefOk returns the zero value for that type. The boolean value indicates if the zero value returned is because the reference.
func SafeCast ¶
SafeCast casts the input object to the specified type, and returns either, the result of the cast, or a zero value for the specified type.
func SafeDo ¶
func SafeDo[T any](op func() T) (out T)
SafeDo calls the function when the operation is non-nil, and returns either the output of the function or the zero value of T.
func SafeWrap ¶
func SafeWrap(op func()) func()
SafeWrap wraps an operation with SafeCall so that the resulting operation is never nil, and will never panic if the input operation is nil.
func UnlessCall ¶ added in v0.10.3
func UnlessCall(cond bool, op func())
UnlessCall is inverse form of WhenCall, calling the provided function only when the conditional is false. Panics if the function is nil.
func UnlessDo ¶ added in v0.10.3
UnlessDo is the inverse form of WhenDo, calling the function only when the condition is false. Panics if the function is nil.
func WhenApply ¶
WhenApply runs the function with the supplied argument only when the condition is true. Panics if the function is nil.
func WhenApplyFuture ¶ added in v0.10.5
WhenApplyFuture resolves the future and calls the operation function only when the conditional is true.
func WhenCall ¶
func WhenCall(cond bool, op func())
WhenCall runs a function when condition is true, and is a noop otherwise. Panics if the function is nil.
func WhenDo ¶
WhenDo calls the function when the condition is true, and returns the result, or if the condition is false, the operation is a noop, and returns zero-value for the type. Panics if the function is nil.
func WhenHandle ¶
WhenHandle passes the argument "in" to the operation IF the condition function (which also takes "in") returns true. Panics if the function is nil.
func WithContext ¶ added in v0.10.3
WithContext runs the function, which is the same type as a fun.Operation, with a new context that is canceled after the function exits.
func WithTimeout ¶ added in v0.10.3
WithTimeout runs the function, which is the same type as fun.Operation, with a new context that expires after the specified duration.
Types ¶
This section is empty.