parallel

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package parallel provides concurrent, context-aware sequence transforms for Go 1.23 iter.Seq values.

The parallel package is useful when each item in a sequence needs independent CPU or I/O work and you want to spread that work across a fixed number of workers. It keeps the same lazy iteration style as the rest of the library while letting callers control concurrency with standard context cancellation.

Example:

ctx := context.Background()
numbers := slices.Values([]int{1, 2, 3, 4})

for v := range parallel.ParallelMap(ctx, numbers, func(n int) int {
	return n * 2
}, 2) {
	fmt.Println(v)
}

Any important notes about behaviour such as laziness, context cancellation, memory usage. ParallelMap and WorkerPoolMap may yield results out of order when multiple workers are used, and they stop quickly when the context is cancelled or the consumer stops early. BatchMap preserves batch order and does not spin up worker goroutines by itself.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BatchMap

func BatchMap[T, U any](ctx context.Context, seq iter.Seq[T], fn func([]T) []U, batchSize int) iter.Seq[U]

BatchMap groups items into batches of size n and processes each batch.

Example
package main

import (
	"context"
	"fmt"
	"slices"

	"github.com/MostafaMagdSalama/vortex/parallel"
)

func main() {
	numbers := slices.Values([]int{1, 2, 3, 4})

	for v := range parallel.BatchMap(context.Background(), numbers, func(batch []int) []int {
		out := make([]int, len(batch))
		for i, n := range batch {
			out[i] = n * 10
		}
		return out
	}, 2) {
		fmt.Println(v)
	}
}
Output:
10
20
30
40

func ParallelMap

func ParallelMap[T, U any](ctx context.Context, seq iter.Seq[T], fn func(T) U, workers int) iter.Seq[U]

ParallelMap processes each element concurrently with n workers.

Example
package main

import (
	"context"
	"fmt"
	"slices"

	"github.com/MostafaMagdSalama/vortex/parallel"
)

func main() {
	numbers := slices.Values([]int{1, 2, 3})

	for v := range parallel.ParallelMap(context.Background(), numbers, func(n int) int {
		return n * 2
	}, 1) {
		fmt.Println(v)
	}
}
Output:
2
4
6

func WorkerPoolMap

func WorkerPoolMap[T, U any](ctx context.Context, seq iter.Seq[T], fn func(T) U, workers int) iter.Seq[U]

WorkerPoolMap uses a fixed pool of workers to process items.

Example
package main

import (
	"context"
	"fmt"
	"slices"

	"github.com/MostafaMagdSalama/vortex/parallel"
)

func main() {
	numbers := slices.Values([]int{1, 2, 3})

	for v := range parallel.WorkerPoolMap(context.Background(), numbers, func(n int) int {
		return n * 3
	}, 1) {
		fmt.Println(v)
	}
}
Output:
3
6
9

Types

This section is empty.

Jump to

Keyboard shortcuts

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