fn

package
v0.14.5 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: Apache-2.0 Imports: 7 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorHandler added in v0.13.0

func ErrorHandler[T any](eh Handler[error]) func(T, error) T

ErrorHandler creates a function that can be used to wrap a function call that returns a value and an error, to simplify some call sites.

Types

type Converter added in v0.13.0

type Converter[I, O any] func(I) O

Converter describes a function that takes a value of one type and returns a value of another type. provides a simplified version of the fun.Converter type (without contexts or errors). The Converter type provides a number of higher level operation over the function types with the provided methods.

func MakeConverter added in v0.13.0

func MakeConverter[I, O any](f func(I) O) Converter[I, O]

MakeConverter creates a new Converter from a function that transforms input type I to output type O.

func (Converter[I, O]) Convert added in v0.13.0

func (cf Converter[I, O]) Convert(in I) O

Convert applies the converter function to the input value and returns the converted output. This is no different than running the converter directly.

func (Converter[I, O]) If added in v0.13.0

func (cf Converter[I, O]) If(cond bool) Converter[I, O]

If returns a converter that executes only when the condition is true, otherwise is a noop and returns zero value of the output type..

func (Converter[I, O]) Iterator added in v0.14.0

func (cf Converter[I, O]) Iterator(seq iter.Seq[I]) iter.Seq[O]

Iterator returns an iterator that yields converted values from the input iterator into the output type. This is an equivalent operation to `irt.Convert`.

func (Converter[I, O]) Lock added in v0.13.0

func (cf Converter[I, O]) Lock() Converter[I, O]

Lock returns a converter that protects the execution of the conversion with a new mutex, ensuring that only one conversion runs at a time.

func (Converter[I, O]) PostFilter added in v0.13.0

func (cf Converter[I, O]) PostFilter(f Filter[O]) Converter[I, O]

PostFilter returns a converter that applies the provided filter to the output after executing the conversion.

func (Converter[I, O]) PostHook added in v0.13.0

func (cf Converter[I, O]) PostHook(h func()) Converter[I, O]

PostHook returns a converter that executes the provided hook function after the conversion using defer, ensuring that the hook always runs. Nil hook functions are ignored.

func (Converter[I, O]) PreFilter added in v0.13.0

func (cf Converter[I, O]) PreFilter(f Filter[I]) Converter[I, O]

PreFilter returns a converter that applies the provided filter to the input before executing the conversion.

func (Converter[I, O]) PreHook added in v0.13.0

func (cf Converter[I, O]) PreHook(h func()) Converter[I, O]

PreHook returns a converter that executes the provided hook function before the conversion.

func (Converter[I, O]) When added in v0.13.0

func (cf Converter[I, O]) When(c func() bool) Converter[I, O]

When returns a converter that executes only when the provided condition function returns true.

func (Converter[I, O]) WithContext added in v0.13.1

func (cf Converter[I, O]) WithContext() func(context.Context, I) (O, error)

WithContext returns a wrapped version of the converter function that has he same signature as fnx.Converter functions.

func (Converter[I, O]) WithLock added in v0.13.0

func (cf Converter[I, O]) WithLock(m *sync.Mutex) Converter[I, O]

WithLock returns a converter that protects the execution of the converter with the provided mutex.

func (Converter[I, O]) WithLocker added in v0.13.0

func (cf Converter[I, O]) WithLocker(m sync.Locker) Converter[I, O]

WithLocker returns a converter that protects the execution of the conversion with the provided sync.Locker instance.

type Filter added in v0.13.0

type Filter[T any] Converter[T, T]

Filter describes a common function object and provides higher level operations on the functions themselves. These functions take an object and returns a value of the same type. This is a special case of the Converter type.

func MakeFilter added in v0.13.0

func MakeFilter[T any](fl func(T) T) Filter[T]

MakeFilter creates a new Filter from a function that transforms a value of type T to another value of the same type.

func (Filter[T]) Apply added in v0.13.0

func (fl Filter[T]) Apply(v T) T

Apply executes the filter function on the provided value and returns the filtered result.

func (Filter[T]) If added in v0.13.0

func (fl Filter[T]) If(cond bool) Filter[T]

If returns a filter that only executes when the condition is true, otherwise the filter will return the input as is.

func (Filter[T]) Join added in v0.13.0

func (fl Filter[T]) Join(fls ...Filter[T]) Filter[T]

Join returns a filter that applies this filter followed by all the provided filters in order. Nil filters cause panics.

func (Filter[T]) Lock added in v0.13.0

func (fl Filter[T]) Lock() Filter[T]

Lock returns a filter that executes the filter operation using a new mutex, ensuring only one instance of the filter runs at a time.

func (Filter[T]) PostHook added in v0.13.0

func (fl Filter[T]) PostHook(op func()) Filter[T]

PostHook returns a filter that executes the provided hook function after applying the filter.

func (Filter[T]) PreHook added in v0.13.0

func (fl Filter[T]) PreHook(op func()) Filter[T]

PreHook returns a filter that executes the provided hook function before applying the filter.

func (Filter[T]) Ptr added in v0.13.0

func (fl Filter[T]) Ptr(v *T)

Ptr applies the filter to the value pointed to by the pointer, modifying it in place.

func (Filter[T]) WithLock added in v0.13.0

func (fl Filter[T]) WithLock(mu *sync.Mutex) Filter[T]

WithLock returns a filter that protects the execution of the filter operation with the provided mutex.

func (Filter[T]) WithNext added in v0.13.0

func (fl Filter[T]) WithNext(next Filter[T]) Filter[T]

WithNext returns a filter that applies this filter first, then applies the next filter to the result.

type Future

type Future[T any] func() T

Future is a basic function for providing a fun-style function type for a function object that will produce an object of the specified type.

func AsFuture

func AsFuture[T any](in T) Future[T]

AsFuture wraps a value and returns a future object that, when called, will return the provided value.

func MakeFuture

func MakeFuture[T any](f func() T) Future[T]

MakeFuture constructs a Future[T] object from a function object.

func (Future[T]) If

func (f Future[T]) If(cond bool) Future[T]

If produces a future that only runs when the condition value is true. If the condition is false, the future will return the zero value of T.

func (Future[T]) Ignore

func (f Future[T]) Ignore() func()

Ignore produces a function that will call the Future but discards the output.

func (Future[T]) Join

func (f Future[T]) Join(merge func(T, T) T, ops ...Future[T]) Future[T]

Join iteratively merges a collection of future operations.

If any of the futures are nil, the zero value for T is used. The merge function MUST NOT be nil.

func (Future[T]) Limit

func (f Future[T]) Limit(in int) Future[T]

Limit returns a future that will call the inner future exactly the specified number of times, and return the last value provided thereafter.

func (Future[T]) Lock

func (f Future[T]) Lock() Future[T]

Lock returns a wrapped future that ensure that all calls to the future are protected by a mutex.

func (Future[T]) Once

func (f Future[T]) Once() Future[T]

Once returns a future that will only run the underlying future exactly once.

func (Future[T]) PostHook

func (f Future[T]) PostHook(fn func()) Future[T]

PostHook unconditionally runs the provided function after running the future. The hook runs in a defer statement.

func (Future[T]) PreHook

func (f Future[T]) PreHook(fn func()) Future[T]

PreHook unconditionally runs the provided function before running and returning the function.

func (Future[T]) RecoverPanic

func (f Future[T]) RecoverPanic() (out T, err error)

RecoverPanic resolves the future, catching any panics and converting them to errors.

func (Future[T]) Reduce

func (f Future[T]) Reduce(merge func(T, T) T, next Future[T]) Future[T]

Reduce takes the input future, the next future, and merges the results using the merge function.

func (Future[T]) Resolve

func (f Future[T]) Resolve() T

Resolve executes the future and returns its value.

func (Future[T]) Slice

func (f Future[T]) Slice() func() []T

Slice returns a future-like function that wraps the output of the future as the first element in a slice.

func (Future[T]) TTL

func (f Future[T]) TTL(dur time.Duration) Future[T]

TTL returns a future that will cache (and return) the value returned by the inner future, for the period described by the value of dur.

func (Future[T]) When

func (f Future[T]) When(c func() bool) Future[T]

When produces a new future wrapping the input future that executes when the condition function returns true, returning the zero value for T when the condition is false. The condition value is checked every time the future is called.

func (Future[T]) WithLock

func (f Future[T]) WithLock(m *sync.Mutex) Future[T]

WithLock return a future that is protected with the provided mutex.

func (Future[T]) WithLocker

func (f Future[T]) WithLocker(m sync.Locker) Future[T]

WithLocker return a future that is protected with the provided sync.Locker implementation.

type Handler

type Handler[T any] func(T)

Handler describes a function that operates on a single object, but returns no output, and is used primarily for side effects, particularly around handling errors or collecting metrics. The Handler implementation here makes it possible to provide panic-safety for these kinds of functions or easily convert to other related types.

func JoinHandlers

func JoinHandlers[T any](seq iter.Seq[Handler[T]]) Handler[T]

JoinHandlers returns a function that combines a collection of handlers.

func NewHandler

func NewHandler[T any](in func(T)) Handler[T]

NewHandler produces an Handler[T] function as a helper.

func NewNoopHandler added in v0.13.0

func NewNoopHandler[T any]() Handler[T]

NewNoopHandler creates a handler function that performs no action.

func (Handler[T]) All

func (of Handler[T]) All(in ...T)

All processes all inputs with the specified handler.

func (Handler[T]) Filter

func (of Handler[T]) Filter(filter func(T) T) Handler[T]

Filter creates an handler that only executes the root handler Use this to process or transform the input before it is passed to the underlying handler. Use in combination with the Skip function to filter out non-actionable inputs.

func (Handler[T]) If

func (of Handler[T]) If(cond bool) Handler[T]

If returns an handler that only executes the root handler if the condition is true.

func (Handler[T]) Job added in v0.14.0

func (pf Handler[T]) Job(in T) func(context.Context) error

Job converts a Handler into a panic-safe worker function suitable for use in worker pool implementations (e.g., wpa.RunWithPool). The returned function catches any panics that occur during execution and converts them to errors.

func (Handler[T]) Join

func (of Handler[T]) Join(chain ...Handler[T]) Handler[T]

Join calls the base handler, and then calls every handler in the chain.

func (Handler[T]) Lock

func (of Handler[T]) Lock() Handler[T]

Lock returns an handler that is protected by a mutex. All execution's of the handler are isolated.

func (Handler[T]) Once

func (of Handler[T]) Once() Handler[T]

Once produces an handler function that runs exactly once, and successive executions of the handler are noops.

func (Handler[T]) PreHook

func (of Handler[T]) PreHook(prev Handler[T]) Handler[T]

PreHook provides the inverse operation to Join, running "prev" handler before the base handler.

Returns itself if prev is nil.

func (Handler[T]) Read added in v0.13.0

func (of Handler[T]) Read(in T)

Read provides a more expository operation to call a handler function.

func (Handler[T]) RecoverPanic

func (of Handler[T]) RecoverPanic(in T) (err error)

RecoverPanic runs the handler function with a panic handler and converts a possible panic to an error.

func (Handler[T]) Skip

func (of Handler[T]) Skip(hook func(T) bool) Handler[T]

Skip runs a check before passing the object to the obsever, when the condition function--which can inspect the input object--returns true, the underlying Handler is called, otherwise, the observation is a noop.

func (Handler[T]) When

func (of Handler[T]) When(cond func() bool) Handler[T]

When returns an handler function that only executes the handler function if the condition function returns true. The condition function is run every time the handler function runs.

func (Handler[T]) WithLock

func (of Handler[T]) WithLock(mtx *sync.Mutex) Handler[T]

WithLock protects the action of the handler with the provied mutex.

func (Handler[T]) WithLocker

func (of Handler[T]) WithLocker(mtx sync.Locker) Handler[T]

WithLocker protects the action of the handler with the provied mutex.

func (Handler[T]) WithNext added in v0.13.0

func (of Handler[T]) WithNext(next Handler[T]) Handler[T]

WithNext creates an handler function that runs both the root handler and the "next" handler.

func (Handler[T]) WithRecover

func (of Handler[T]) WithRecover(oe Handler[error]) Handler[T]

WithRecover handles any panic encountered during the handler's execution and converts it to an error.

Jump to

Keyboard shortcuts

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