ft

package
v0.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cast

func Cast[T any](in any) (v T, ok bool)

Cast is the same as doing `v, ok := in.(t)`, but with more clarity at the call site,

func Contains

func Contains[T comparable](item T, slice []T) bool

Contain returns true if an element of the slice is equal to the item.

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 DoTimes

func DoTimes(n int, op func())

DoTimes runs the specified option n times.

func Flip

func Flip[A any, B any](first A, second B) (B, A)

Flip takes two arguments and returns them in the opposite order. Intended to wrap other functions to reduce the friction when briding APIs

func IgnoreFirst

func IgnoreFirst[A any, B any](first A, second B) B

IgnoreFirst takes two arguments and returns only the second, for use in wrapping functions that return two values.

func IgnoreSecond

func IgnoreSecond[A any, B any](first A, second B) A

IgnoreSecond takes two arguments and returns only the first, for use when wrapping functions that return two values.

func IsOK

func IsOK[T any](_ T, ok bool) bool

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 IsType

func IsType[T any](in any) bool

IsType checks if the type of the argument matches the type specifier.

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 Must

func Must[T any](arg T, err error) T

Must wraps a function that returns a value and an error, and converts the error to a panic.

func MustBeOk

func MustBeOk[T any](out T, ok bool) T

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 ok) { 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 runs a function, exactly once, to produce a value which is then cached, and returned on any successive calls to the function.

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

func RefOK[T any](in *T) (value T, ok bool)

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 SafeCall

func SafeCall(op func())

SafeCall only calls the operation when it's non-nil.

func SafeCast

func SafeCast[T any](in any) T

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) T

SafeDo calls the function when the operation is non-nil, and returns either the output of the function or

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 WhenApply

func WhenApply[T any](cond bool, op func(T), arg T)

WhenApply runs the function with the supplied argument only when the condition is true.

func WhenCall

func WhenCall(cond bool, op func())

WhenCall runs a function when condition is true, and is a noop otherwise.

func WhenDo

func WhenDo[T any](cond bool, op func() T) (out T)

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.

func WhenHandle

func WhenHandle[T any](in T, cond func(T) bool, op func(T))

WhenHandle passes the argument "in" to the operation IF the condition function (which also takes "in") returns true.

func Wrapper

func Wrapper[T any](in T) func() T

Wrapper produces a function that always returns the value provided. Useful for bridging interface paradigms, and for storing interface-typed objects in atomics.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL