Documentation
¶
Overview ¶
Package pargo provides generic, context-aware primitives for processing collections concurrently.
Pargo offers simple, production-ready APIs for common collection operations, including Map, Filter, and Reduce. All operations support context cancellation, configurable worker pools, and deterministic ordering where applicable.
Example:
result, err := pargo.Map(ctx, items, mapper)
By default, Pargo uses runtime.GOMAXPROCS(0) worker goroutines.
Index ¶
- func Filter[T any](ctx context.Context, items []T, fn FilterFunc[T], opts ...Option) ([]T, error)
- func FlatMap[T any, R any](ctx context.Context, items []T, fn FlatMapFunc[T, R], opts ...Option) ([]R, error)
- func Map[T any, R any](ctx context.Context, items []T, fn MapFunc[T, R], opts ...Option) ([]R, error)
- func Reduce[T any](ctx context.Context, items []T, initial T, fn ReduceFunc[T], opts ...Option) (T, error)
- type FilterFunc
- type FlatMapFunc
- type MapFunc
- type Option
- type ReduceFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Filter ¶ added in v0.2.0
func Filter[T any]( ctx context.Context, items []T, fn FilterFunc[T], opts ...Option, ) ([]T, error)
Filter evaluates fn for each element of items concurrently and returns a new slice containing only the elements for which fn returns true.
The relative order of retained elements matches their order in the input slice.
If any invocation of fn returns an error, processing stops, the remaining work is canceled, and the error is returned.
The number of workers can be configured using WithWorkers.
func FlatMap ¶ added in v0.4.0
func FlatMap[T any, R any]( ctx context.Context, items []T, fn FlatMapFunc[T, R], opts ...Option, ) ([]R, error)
FlatMap applies fn concurrently to every element and flattens the results into a single slice while preserving the input order.
If any invocation of fn returns an error, processing stops, the remaining work is canceled, and the error is returned.
The number of workers can be configured using WithWorkers.
func Map ¶
func Map[T any, R any]( ctx context.Context, items []T, fn MapFunc[T, R], opts ...Option, ) ([]R, error)
Map applies fn to each element of items concurrently and returns a new slice containing the transformed values.
The output slice preserves the order of the input slice regardless of the order in which workers complete.
If any invocation of fn returns an error, processing stops, the remaining work is canceled, and the error is returned.
The number of workers can be configured using WithWorkers. By default, Map uses runtime.GOMAXPROCS(0) workers.
func Reduce ¶ added in v0.3.0
func Reduce[T any]( ctx context.Context, items []T, initial T, fn ReduceFunc[T], opts ...Option, ) (T, error)
Reduce combines the elements of items into a single value using fn.
Reduction is performed concurrently by first reducing independent chunks and then combining the partial results.
For deterministic results, fn should be associative (for example, addition, multiplication, minimum, or maximum). Reducers whose results depend on evaluation grouping or side effects may produce different results than a purely sequential reduction.
If fn returns an error, processing stops and the error is returned.
Types ¶
type FilterFunc ¶ added in v0.2.0
FilterFunc determines whether an element should be retained.
Returning true keeps the element in the output slice. Returning false discards it.
type FlatMapFunc ¶ added in v0.4.0
type MapFunc ¶
MapFunc transforms a value of type T into a value of type R.
The index parameter corresponds to the element's position in the input slice.
type Option ¶
type Option func(*config)
Option configures the behavior of a Pargo operation.
func WithWorkers ¶
WithWorkers specifies the maximum number of worker goroutines used by an operation.
Values less than one are ignored, causing the default worker count to be used instead.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
|
|
|
cancellation
command
|
|
|
csv-analyzer
command
|
|
|
filter
command
|
|
|
flatmap
command
|
|
|
ordered
command
|
|
|
reduce
command
|
|
|
workers
command
|