Documentation
¶
Index ¶
- func All[T any](in []T, fn func(T) bool) bool
- func Contain[In comparable](in []In, v In) bool
- func ContainWithFn[In any](in []In, fn func(In) bool) bool
- func First[In any](in []In, fn func(In) bool) *In
- func Flat[T any](in [][]T) []T
- func ForEach[T any](in []T, fn func(T) error) error
- func Keys[K comparable, V any](in map[K]V) []K
- func Last[In any](in []In, fn func(In) bool) *In
- func Pipe[In, Out any](input []In, fns ...PipeFn) ([]Out, error)
- func Reduce[In any, Out any](in []In, fn func(accumulator Out, v In) Out, init Out) Out
- func SliceFilter[T any](in []T, fn func(T) bool) []T
- func SliceInsertFirst[T any](slice []T, elem T) []T
- func SliceInsertLast[T any](slice []T, elem T) []T
- func SliceMap[In any, Out any](in []In, fn func(In) Out) []Out
- func SliceMapWithError[In any, Out any](in []In, fn func(In) (Out, error)) ([]Out, error)
- func Some[T any](in []T, fn func(T) bool) bool
- func Sum[T constraints.Integer | constraints.Float](acc T, v any) T
- func ToLookupTable[KeyType comparable, ElemType any](in []ElemType, keyFn func(ElemType) KeyType) map[KeyType]ElemType
- func Values[K comparable, V any](in map[K]V) []V
- type BarrierFn
- type Collection
- func (col Collection[T]) Filter(fn func(any) bool) Collection[T]
- func (c Collection[T]) First() (*T, error)
- func (c Collection[T]) ForEach(fn func(item T) error) error
- func (c Collection[T]) Last() (*T, error)
- func (col Collection[T]) Map(fn func(any) any) Collection[T]
- func (col Collection[T]) MapWithError(fn func(any) (any, error)) Collection[T]
- func (c Collection[T]) Pick(rand int) (*T, error)
- func (c Collection[T]) Reduce(fn func(accumulator T, current any) T, initialValue T) (T, error)
- func (c Collection[T]) ToSlice() ([]T, error)
- type ElemFn
- func LazyFilter[T any](fn func(T) bool) ElemFn
- func LazyFilterMap[In, Out any](fn func(In) (Out, bool)) ElemFn
- func LazyMap[In, Out any](fn func(In) Out) ElemFn
- func LazyMapWithError[In, Out any](fn func(In) (Out, error)) ElemFn
- func LazyTap[T any](fn func(T)) ElemFn
- func LazyTapWithError[T any](fn func(T) error) ElemFn
- type LazyOption
- type LazyPipeline
- func (lp *LazyPipeline[In, Out]) Elem(fns ...ElemFn) *LazyPipeline[In, Out]
- func (lp *LazyPipeline[In, Out]) Once(fn func() error) *LazyPipeline[In, Out]
- func (lp *LazyPipeline[In, Out]) Pipe(fns ...BarrierFn) *LazyPipeline[In, Out]
- func (lp *LazyPipeline[In, Out]) Run(opts ...LazyOption) ([]Out, error)
- type Pair
- type PipeFn
- func Cons[T any](elem T) PipeFn
- func Filter[T any](fn func(T) bool) PipeFn
- func InsertFirst[T any](elem T) PipeFn
- func InsertLast[T any](elem T) PipeFn
- func Map[In, Out any](fn func(In) Out) PipeFn
- func MapWithError[In, Out any](fn func(In) (Out, error)) PipeFn
- func Once(fn func() error) PipeFn
- func OnceWith[T any](fn func([]T) error) PipeFn
- func Tap[T any](fn func(T)) PipeFn
- func TapWithError[T any](fn func(T) error) PipeFn
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contain ¶
func Contain[In comparable](in []In, v In) bool
func ContainWithFn ¶
func Keys ¶
func Keys[K comparable, V any](in map[K]V) []K
func Pipe ¶
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 SliceFilter ¶
func SliceInsertFirst ¶
func SliceInsertFirst[T any](slice []T, elem T) []T
func SliceInsertLast ¶
func SliceInsertLast[T any](slice []T, elem T) []T
func SliceMapWithError ¶
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.
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)
type ElemFn ¶ added in v2.0.2
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
LazyFilter returns an ElemFn that keeps only elements satisfying the predicate.
func LazyFilterMap ¶ added in v2.0.2
LazyFilterMap returns an ElemFn that transforms and optionally filters elements. If fn returns false as the second value, the element is excluded.
func LazyMapWithError ¶ added in v2.0.2
LazyMapWithError returns an ElemFn that transforms each element using fn, propagating any error to abort the pipeline.
func LazyTap ¶ added in v2.0.2
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
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 ¶
func InsertFirst ¶
InsertFirst returns a PipeFn that prepends the given element to the slice.
func InsertLast ¶
InsertLast returns a PipeFn that appends the given element to the slice.
func MapWithError ¶
func Once ¶ added in v2.0.2
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
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
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
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.