functional

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](in []T, fn func(T) bool) bool

func Contain

func Contain[In comparable](in []In, v In) bool

func ContainWithFn

func ContainWithFn[In any](in []In, fn func(In) bool) bool

func First

func First[In any](in []In, fn func(In) bool) *In

func Flat

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

func ForEach

func ForEach[T any](in []T, fn func(T) error) error

func Keys

func Keys[K comparable, V any](in map[K]V) []K

func Last

func Last[In any](in []In, fn func(In) bool) *In

func Pipe

func Pipe[In, Out any](input []In, fns ...PipeFn) ([]Out, error)

example functional.Pipe[int, string](

[]int{1, 2, 3},
functional.Filter(func(i int) bool { return i > 1 }),
functional.Map(func(i int) string { return strconv.Itoa(i * 10) }),
functional.MapWithError(func(s string) (string, error) { return s + "!", nil }),

) // return []string{"20!", "30!"}, nil

func Reduce

func Reduce[In any, Out any](in []In, fn func(accumulator Out, v In) Out, init Out) Out

func SliceFilter

func SliceFilter[T any](in []T, fn func(T) bool) []T

func SliceInsertFirst

func SliceInsertFirst[T any](slice []T, elem T) []T

func SliceInsertLast

func SliceInsertLast[T any](slice []T, elem T) []T

func SliceMap

func SliceMap[In any, Out any](in []In, fn func(In) Out) []Out

func SliceMapWithError

func SliceMapWithError[In any, Out any](in []In, fn func(In) (Out, error)) ([]Out, error)

func Some

func Some[T any](in []T, fn func(T) bool) bool

func Sum

func Sum[T constraints.Integer | constraints.Float](acc T, v any) T

example for reduce

func ToLookupTable

func ToLookupTable[KeyType comparable, ElemType any](in []ElemType, keyFn func(ElemType) KeyType) map[KeyType]ElemType

func Values

func Values[K comparable, V any](in map[K]V) []V

Types

type BarrierFn added in v2.0.2

type BarrierFn struct {
	// contains filtered or unexported fields
}

BarrierFn wraps a PipeFn with type-safe []any ↔ []T converters, capturing type information at construction time via generics so that no reflect is needed at execution time.

func Barrier added in v2.0.2

func Barrier[In, Out any](fn PipeFn) BarrierFn

Barrier wraps a PipeFn for use in a LazyPipeline. The type parameters capture the input and output element types, enabling type-safe conversion between []any and typed slices without reflect.

type Collection

type Collection[T any] struct {
	// contains filtered or unexported fields
}

func From

func From[From, To any](from []From) Collection[To]

func (Collection[T]) Filter

func (col Collection[T]) Filter(fn func(any) bool) Collection[T]

func (Collection[T]) First

func (c Collection[T]) First() (*T, error)

func (Collection[T]) ForEach

func (c Collection[T]) ForEach(fn func(item T) error) error

func (Collection[T]) Last

func (c Collection[T]) Last() (*T, error)

func (Collection[T]) Map

func (col Collection[T]) Map(fn func(any) any) Collection[T]

func (Collection[T]) MapWithError

func (col Collection[T]) MapWithError(fn func(any) (any, error)) Collection[T]

func (Collection[T]) Pick

func (c Collection[T]) Pick(rand int) (*T, error)

func (Collection[T]) Reduce

func (c Collection[T]) Reduce(fn func(accumulator T, current any) T, initialValue T) (T, error)

func (Collection[T]) ToSlice

func (c Collection[T]) ToSlice() ([]T, error)

aggregator

type ElemFn added in v2.0.2

type ElemFn func(elem any) (output any, keep bool, err error)

ElemFn is an element-level transformation function that unifies map, filter, and map+error into a single signature.

  • output: the transformed element
  • keep: false to filter out the element
  • err: non-nil to abort the pipeline

func LazyFilter added in v2.0.2

func LazyFilter[T any](fn func(T) bool) ElemFn

LazyFilter returns an ElemFn that keeps only elements satisfying the predicate.

func LazyFilterMap added in v2.0.2

func LazyFilterMap[In, Out any](fn func(In) (Out, bool)) ElemFn

LazyFilterMap returns an ElemFn that transforms and optionally filters elements. If fn returns false as the second value, the element is excluded.

func LazyMap added in v2.0.2

func LazyMap[In, Out any](fn func(In) Out) ElemFn

LazyMap returns an ElemFn that transforms each element using fn.

func LazyMapWithError added in v2.0.2

func LazyMapWithError[In, Out any](fn func(In) (Out, error)) ElemFn

LazyMapWithError returns an ElemFn that transforms each element using fn, propagating any error to abort the pipeline.

func LazyTap added in v2.0.2

func LazyTap[T any](fn func(T)) ElemFn

LazyTap returns an ElemFn that applies a side-effect function to each element without modifying it. Useful for logging, debugging, or metrics collection.

When used with WithWorkers(n), fn may be called from multiple goroutines concurrently. The caller is responsible for ensuring fn is goroutine-safe. WithOrdered(true) guarantees output order but not side-effect invocation order.

func LazyTapWithError added in v2.0.2

func LazyTapWithError[T any](fn func(T) error) ElemFn

LazyTapWithError returns an ElemFn that applies a side-effect function to each element without modifying it. If fn returns a non-nil error, the pipeline is aborted.

When used with WithWorkers(n), fn may be called from multiple goroutines concurrently. The caller is responsible for ensuring fn is goroutine-safe.

type LazyOption added in v2.0.2

type LazyOption func(*lazyConfig)

LazyOption configures the execution behavior of a LazyPipeline.

func WithChunkSize added in v2.0.2

func WithChunkSize(size int) LazyOption

WithChunkSize sets the barrier chunk size for parallel execution. 0 means automatic sizing.

func WithContext added in v2.0.2

func WithContext(ctx context.Context) LazyOption

WithContext sets the context for the pipeline execution. The context is checked between elements and can cancel parallel workers.

func WithOrdered added in v2.0.2

func WithOrdered(ordered bool) LazyOption

WithOrdered controls whether parallel execution preserves input order. Default is true.

func WithParallelThreshold added in v2.0.2

func WithParallelThreshold(n int) LazyOption

WithParallelThreshold sets the minimum number of elements required for parallel execution. Below this threshold, sequential execution is used. Default is 1024.

func WithWorkers added in v2.0.2

func WithWorkers(n int) LazyOption

WithWorkers sets the number of parallel workers for element-level stages. 0 or 1 means sequential execution.

type LazyPipeline added in v2.0.2

type LazyPipeline[In, Out any] struct {
	// contains filtered or unexported fields
}

LazyPipeline is a deferred execution pipeline that collects stages and executes them on Run(). Consecutive element-level stages are fused into a single loop to avoid intermediate slice allocations.

func Lazy added in v2.0.2

func Lazy[In, Out any](input []In) *LazyPipeline[In, Out]

Lazy creates a new lazy pipeline with the given input slice.

func (*LazyPipeline[In, Out]) Elem added in v2.0.2

func (lp *LazyPipeline[In, Out]) Elem(fns ...ElemFn) *LazyPipeline[In, Out]

Elem appends element-level transformation stages to the pipeline. Consecutive Elem stages are fused into a single loop during execution.

func (*LazyPipeline[In, Out]) Once added in v2.0.2

func (lp *LazyPipeline[In, Out]) Once(fn func() error) *LazyPipeline[In, Out]

Once appends a barrier that executes fn exactly once (not per-element). It forces materialization of preceding Elem stages. Useful for side-effects (DB queries, logging) whose results are captured via closures for subsequent stages.

func (*LazyPipeline[In, Out]) Pipe added in v2.0.2

func (lp *LazyPipeline[In, Out]) Pipe(fns ...BarrierFn) *LazyPipeline[In, Out]

Pipe appends slice-level transformation stages (barriers) to the pipeline. Each barrier forces materialization of preceding element-level stages. Use Barrier[In, Out](pipeFn) to wrap an existing PipeFn.

func (*LazyPipeline[In, Out]) Run added in v2.0.2

func (lp *LazyPipeline[In, Out]) Run(opts ...LazyOption) ([]Out, error)

Run executes the lazy pipeline and returns the final result.

type Pair

type Pair[K comparable, V any] struct {
	Key   K
	Value V
}

func Entries

func Entries[K comparable, V any](in map[K]V) []Pair[K, V]

type PipeFn

type PipeFn func(any) (any, error)

func Cons

func Cons[T any](elem T) PipeFn

Cons is an alias for InsertFirst (classic FP "cons" operation).

func Filter

func Filter[T any](fn func(T) bool) PipeFn

func InsertFirst

func InsertFirst[T any](elem T) PipeFn

InsertFirst returns a PipeFn that prepends the given element to the slice.

func InsertLast

func InsertLast[T any](elem T) PipeFn

InsertLast returns a PipeFn that appends the given element to the slice.

func Map

func Map[In, Out any](fn func(In) Out) PipeFn

func MapWithError

func MapWithError[In, Out any](fn func(In) (Out, error)) PipeFn

func Once added in v2.0.2

func Once(fn func() error) PipeFn

Once returns a PipeFn that executes fn once, passing the slice through unchanged. No type parameter needed — the slice is not accessed.

func OnceWith added in v2.0.2

func OnceWith[T any](fn func([]T) error) PipeFn

OnceWith returns a PipeFn that executes fn with access to the current typed slice. The slice passes through unchanged.

func Tap added in v2.0.2

func Tap[T any](fn func(T)) PipeFn

Tap returns a PipeFn that applies a side-effect function to each element without modifying the slice. Useful for logging, debugging, or metrics collection.

func TapWithError added in v2.0.2

func TapWithError[T any](fn func(T) error) PipeFn

TapWithError returns a PipeFn that applies a side-effect function to each element without modifying the slice. If fn returns a non-nil error, the pipeline is aborted.

Jump to

Keyboard shortcuts

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