Documentation
¶
Overview ¶
Package ft provides high-level function tools for manipulating common function objects and types.
Index ¶
- func Apply[T any](fn func(T), arg T)
- func ApplyIfElse[T any](cond bool, applyIf func(T), applyElse func(T), arg T)
- func ApplyMany[T any](fn func(T), args []T)
- func ApplySafe[T any](fn func(T), arg T)
- func ApplyTimes[T any](n int, fn func(T), arg T)
- func ApplyUnless[T any](cond bool, op func(T), arg T)
- func ApplyWhen[T any](cond bool, op func(T), arg T)
- func Call(op func())
- func CallIfElse(cond bool, doIf func(), doElse func())
- func CallMany(ops []func())
- func CallSafe(op func())
- func CallTimes(n int, op func())
- func CallUnless(cond bool, op func())
- func CallWhen(cond bool, op func())
- func CallWithContext(op func(context.Context))
- func CallWithTimeout(dur time.Duration, op func(context.Context))
- func Cast[T any](in any) (v T, ok bool)
- func Check[T any](value T, err error) (zero T, _ bool)
- func ContextErrorChannel(ctx context.Context) <-chan error
- func Convert[A any, B any](mapper func(A) B, values iter.Seq[A]) iter.Seq[B]
- func Default[T comparable](input T, defaultValue T) T
- func DefaultApply[T comparable, A any](input T, fn func(A) T, arg A) T
- func DefaultFuture[T comparable](input T, fn func() T) T
- func DefaultNew[T any](input *T) *T
- func Do[T any](op func() T) T
- func Do2[T any, V any](op func() (T, V)) (T, V)
- func DoIfElse[T any](cond bool, doIf func() T, doElse func() T) T
- func DoMany[T any](ops []func() T) iter.Seq[T]
- func DoMany2[A any, B any](ops []func() (A, B)) iter.Seq2[A, B]
- func DoSafe[T any](op func() T) (out T)
- func DoTimes[T any](n int, op func() T) iter.Seq[T]
- func DoUnless[T any](cond bool, op func() T) T
- func DoWhen[T any](cond bool, op func() T) T
- func DoWithContext[T any](op func(context.Context) T) (out T)
- func Filter[T any](fn func(T) T, arg T) T
- func FilterIfElse[T any](cond bool, filterIf func(T) T, filterElse func(T) T, arg T) T
- func FilterSafe[T any](fn func(T) T, arg T) T
- func FilterUnless[T any](cond bool, op func(T) T, arg T) T
- func FilterWhen[T any](cond bool, op func(T) T, arg T) T
- func Flip[A any, B any](first A, second B) (B, A)
- func IfElse[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 Is(p bool) bool
- 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 Ptr[T any](in T) *T
- func Recover(ob func(error))
- func Recv[T any](ch <-chan T) T
- func Ref[T any](in *T) T
- func RefOk[T any](in *T) (value T, ok bool)
- func SafeCast[T any](in any) T
- func Send[T any](ch chan<- T, fut func() T)
- func Seq[T any](input T) iter.Seq[T]
- func Seq2[A any, B any](a A, b B) iter.Seq2[A, B]
- func Slice[T any](items ...T) []T
- func Unless[T any](cond bool, value T) (out T)
- func Wait[T any](ch <-chan T)
- func When[T any](cond bool, value T) (out T)
- func WithRecoverApply[T any](op func(T), in T) (err error)
- func WithRecoverCall(fn func()) (err error)
- func WithRecoverDo[T any](fn func() T) (_ T, err error)
- func WithRecoverFilter[T any](op func(T) T, in T) (_ T, err error)
- func Wrap[T any](in T) func() T
- func WrapCheck[T any](value T, err error) func() (T, bool)
- func WrapRecoverApply[T any](op func(T), in T) func() error
- func WrapRecoverCall(fn func()) func() error
- func WrapRecoverDo[T any](fn func() T) func() (T, error)
- func WrapRecoverFilter[T any](op func(T) T) func(T) (T, error)
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)
Apply calls the input function with the provided argument.
func ApplyIfElse ¶ added in v0.13.0
ApplyIfElse passes the argument value to one of the functions depending on the value of the conditional. When cond is true, ApplyIfElse uses the applyIf function; otherwise it uses the applyElse function.
Nil applyIf/applyElse functions are ignored.
func ApplyMany ¶ added in v0.12.0
func ApplyMany[T any](fn func(T), args []T)
ApplyMany calls the function on each of the items in the provided slice. ApplyMany is a noop if the function is nil.
func ApplySafe ¶ added in v0.12.0
func ApplySafe[T any](fn func(T), arg T)
ApplySafe calls the function, fn, on the value, arg, only if the function is not nil.
func ApplyTimes ¶ added in v0.13.0
ApplyTimes runs the provided function with the argument the specified number of times.
func ApplyUnless ¶ added in v0.12.0
ApplyUnless runs the function with the supplied argument only when the condition is true. Panics if the function is nil.
func ApplyWhen ¶ added in v0.12.0
ApplyWhen runs the function with the supplied argument only when the condition is true. Panics if the function is nil.
func CallIfElse ¶ added in v0.12.0
func CallIfElse(cond bool, doIf func(), doElse func())
CallIfElse is, effectively the if-form from (many) lisps: when the condition is true, the first function is called, and otherwise the second. If the function is nil, CallIfElse is a noop. The "other" function call is never called.
func CallMany ¶ added in v0.12.0
func CallMany(ops []func())
CallMany calls each of the provided function, skipping any nil functions.
func CallSafe ¶ added in v0.12.0
func CallSafe(op func())
CallSafe only calls the operation when it's non-nil.
func CallTimes ¶ added in v0.12.0
func CallTimes(n int, op func())
CallTimes runs the specified operation n times.
func CallUnless ¶ added in v0.12.0
func CallUnless(cond bool, op func())
CallUnless is inverse form of CallWhen, calling the provided function only when the conditional is false. Panics if the function is nil.
func CallWhen ¶ added in v0.12.0
func CallWhen(cond bool, op func())
CallWhen runs a function when condition is true, and is a noop otherwise. Panics if the function is nil.
func CallWithContext ¶ added in v0.12.0
CallWithContext runs the function, which is the same type as a fnx.Operation, with a new context that is canceled after the function exits.
func CallWithTimeout ¶ added in v0.12.0
CallWithTimeout runs the function, which is the same type as fnx.Operation, with a new context that expires after the specified duration.
func Check ¶ added in v0.12.0
Check can wrap a the callsite of a function that returns a value and an error, and returns (zero, false) if the error is non, nil, and (value, true) if the error is nil.
func ContextErrorChannel ¶ added in v0.13.0
ContextErrorChannel returns a channel that will receive the context's error when the context is done. The channel is closed after sending the error.
func Convert ¶ added in v0.12.0
Convert takes a sequence of A and converts it, lazily into a sequence of B, using the mapper function.
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 DefaultApply ¶ added in v0.13.0
func DefaultApply[T comparable, A any](input T, fn func(A) T, arg A) T
DefaultApply returns the input if it's not zero, otherwise applies the function to the argument and returns the result.
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 Do ¶ added in v0.12.0
func Do[T any](op func() T) T
Do executes the provided function and returns its result.
func DoIfElse ¶ added in v0.12.0
DoIfElse returns the output of the first function when the condition is false, and the output of the second function otherwise. If the function is nil, DoIfElse returns the zero value for the type. The "other" function call is never called.
func DoMany ¶ added in v0.12.0
DoMany calls each of the provided functions and returns an iterator of their results.
func DoMany2 ¶ added in v0.12.0
DoMany2 calls each of the provided functions and returns an iterator of their results.
func DoSafe ¶ added in v0.12.0
func DoSafe[T any](op func() T) (out T)
DoSafe calls the function when the operation is non-nil, and returns either the output of the function or the zero value of T.
func DoTimes ¶
DoTimes calls the provided function n times and returns an iterator of their results.
func DoUnless ¶ added in v0.12.0
DoUnless is the inverse form of DoWhen, calling the function only when the condition is false. Panics if the function is nil.
func DoWhen ¶ added in v0.12.0
DoWhen 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 DoWithContext ¶ added in v0.12.0
DoWithContext runs a function, which is the same type as fun.Future, with a new context that is canceled after the function returns.
func Filter ¶ added in v0.13.0
func Filter[T any](fn func(T) T, arg T) T
Filter calls the input function with the provided argument and returns the result.
func FilterIfElse ¶ added in v0.13.0
FilterIfElse applies the filter function to the argument based on the condition. If cond is true, filterIf is applied; otherwise filterElse is applied.
func FilterSafe ¶ added in v0.13.0
func FilterSafe[T any](fn func(T) T, arg T) T
FilterSafe passes the arg value through the filter function. If the filter function is nil, then FilterSafe returns the input argument.
func FilterUnless ¶ added in v0.13.0
FilterUnless applies the filter function to the argument only when the condition is false. If the condition is true, returns the argument unchanged.
func FilterWhen ¶ added in v0.13.0
FilterWhen applies the filter function to the argument only when the condition is true. If the condition is false, returns the argument unchanged.
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 IfElse ¶ added in v0.12.0
IfElse 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 Is ¶ added in v0.12.0
Is is a verbose boolean identity function that returns the input boolean unchanged.
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 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 Recover ¶ added in v0.12.0
func Recover(ob func(error))
Recover catches a panic, turns it into an error and passes it to the provided observer function.
func Recv ¶ added in v0.13.0
func Recv[T any](ch <-chan T) T
Recv receives and returns a value from the channel, blocking according to the Go channel semantics.
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 Send ¶ added in v0.13.0
func Send[T any](ch chan<- T, fut func() T)
Send executes the provided future function and sends its result to the channel.
func Slice ¶ added in v0.12.0
func Slice[T any](items ...T) []T
Slice returns a slice for the variadic arguments. Useful for adapting functions that take slice arguments where it's easier to pass values variadicly.
func Unless ¶ added in v0.13.0
Unless returns the value if the condition is false, otherwise returns the zero value for type T.
func Wait ¶ added in v0.13.0
func Wait[T any](ch <-chan T)
Wait blocks until the channel produces a value, always discarding the value.
func When ¶ added in v0.13.0
When returns the value if the condition is true, otherwise returns the zero value for type T.
func WithRecoverApply ¶ added in v0.12.0
WithRecoverApply runs a function with a panic handler that converts the panic to an error.
func WithRecoverCall ¶ added in v0.12.0
func WithRecoverCall(fn func()) (err error)
WithRecoverCall runs a function without arguments that does not produce an error and, if the function panics, converts it into an error.
func WithRecoverDo ¶ added in v0.12.0
WithRecoverDo runs a function with a panic handler that converts the panic to an error.
func WithRecoverFilter ¶ added in v0.13.0
WithRecoverFilter runs a filter function with a panic handler that converts the panic to an error.
func Wrap ¶ added in v0.12.0
func Wrap[T any](in T) func() T
Wrap produces a function that always returns the value provided. Useful for bridging interface paradigms, and for storing interface-typed objects in atomics.
func WrapCheck ¶ added in v0.13.0
WrapCheck returns a function that when called returns the result of Check on the provided value and error.
func WrapRecoverApply ¶ added in v0.13.0
WrapRecoverApply returns a function that calls WithRecoverApply with the provided operation and input.
func WrapRecoverCall ¶ added in v0.12.0
func WrapRecoverCall(fn func()) func() error
WrapRecoverCall wraps a function without arguments that does not produce an error with one that does produce an error. When called, the new function will never panic but returns an error if the input function panics.
func WrapRecoverDo ¶ added in v0.12.0
WrapRecoverDo wraps a function that returns a single value, with one that returns that argument and an error if the underlying function panics.
func WrapRecoverFilter ¶ added in v0.13.0
WrapRecoverFilter wraps a filter function with one that returns the filtered value and an error if the underlying function panics.
Types ¶
This section is empty.