Skip to content

API Reference

All exported symbols belong to package automi (import "github.com/vladimirvivien/automi").

Core Types

type Stream[T any] struct { /* unexported fields */ }

type Source[T any] interface {
    Stream(context.Context) iter.Seq2[T, error]
}

type Sink[T any] interface {
    Consume(context.Context, iter.Seq2[T, error]) error
}

type SinkFunc[T any] func(context.Context, iter.Seq2[T, error]) error
func (f SinkFunc[T]) Consume(ctx context.Context, seq iter.Seq2[T, error]) error

type Pair[A, B any] struct {
    First  A
    Second B
}

type Step[In, Out any] func(context.Context, In) (Out, error)

Sentinel Variables

var (
    ErrSkip        = errors.New("automi: skip item")
    ErrNilSource   = errors.New("automi: nil source")
    ErrNilSink     = errors.New("automi: nil sink")
    ErrNilFunc     = errors.New("automi: nil function")
    ErrNilReader   = errors.New("automi: nil reader")
    ErrNilWriter   = errors.New("automi: nil writer")
    ErrCircuitOpen = errors.New("automi: circuit breaker open")
)

Sources

func From[T any](src Source[T]) Stream[T]
func FromSlice[T any](xs []T) Stream[T]
func FromChan[T any](ch <-chan T) Stream[T]
func FromSeq[T any](seq iter.Seq[T]) Stream[T]
func FromSeq2[T any](seq iter.Seq2[T, error]) Stream[T]
func FromFunc[T any](next func(context.Context) (T, error)) Stream[T]
func FromCSV(r io.Reader, opts ...CSVOption) Stream[[]string]
func FromLines(r io.Reader) Stream[string]
func FromReader(r io.Reader, chunkSize int) Stream[[]byte]

Stream Methods

Synchronous Stages

func (s Stream[T]) Filter(pred func(T) bool) Stream[T]
func (s Stream[T]) Take(n int) Stream[T]
func (s Stream[T]) Drop(n int) Stream[T]
func (s Stream[T]) Tap(fn func(T)) Stream[T]
func (s Stream[T]) Map[U any](fn func(T) U) Stream[U]
func (s Stream[T]) TryMap[U any](fn func(context.Context, T) (U, error)) Stream[U]
func (s Stream[T]) FlatMap[U any](fn func(T) iter.Seq[U]) Stream[U]
func (s Stream[T]) Through[U any](stage func(Stream[T]) Stream[U]) Stream[U]

Concurrency & Flow Control Methods

func (s Stream[T]) ParMap[U any](fn func(context.Context, T) (U, error), opts ...ParOption) Stream[U]
func (s Stream[T]) Buffer(size int, opts ...BufferOption) Stream[T]
func (s Stream[T]) BufferDrop(size int, onDrop func(T), opts ...BufferOption) Stream[T]
func (s Stream[T]) RateLimit(perSec float64, opts ...RateOption) Stream[T]
func (s Stream[T]) Debounce(d time.Duration) Stream[T]

Terminal Methods

func (s Stream[T]) All(ctx context.Context) iter.Seq2[T, error]
func (s Stream[T]) Into(ctx context.Context, sink Sink[T]) error
func (s Stream[T]) Collect(ctx context.Context) ([]T, error)
func (s Stream[T]) Each(ctx context.Context, fn func(context.Context, T) error) error
func (s Stream[T]) ParEach(ctx context.Context, workers int, fn func(context.Context, T) error) error
func (s Stream[T]) Reduce[U any](ctx context.Context, init U, fn func(U, T) U) (U, error)
func (s Stream[T]) GroupBy[K comparable](ctx context.Context, keyFn func(T) K) (map[K][]T, error)

Step Constructors & Methods

func Do[In, Out any](fn func(context.Context, In) (Out, error)) Step[In, Out]
func Chain[T any](steps ...Step[T, T]) Step[T, T]

func (st Step[In, Out]) Then[Next any](next func(context.Context, Out) (Next, error)) Step[In, Next]
func (st Step[In, Out]) Retry(attempts int, initialBackoff time.Duration, opts ...RetryOption) Step[In, Out]
func (st Step[In, Out]) CircuitBreaker(threshold int, resetAfter time.Duration) Step[In, Out]
func (st Step[In, Out]) OnError(handler func(context.Context, In, error) error) Step[In, Out]

Package-Level Combinators

func Batch[T any](s Stream[T], opts ...BatchOption) Stream[[]T]
func Concat[T any](streams ...Stream[T]) Stream[T]
func Merge[T any](streams ...Stream[T]) Stream[T]
func Zip[A, B any](a Stream[A], b Stream[B]) Stream[Pair[A, B]]
func FanOut[T any](ctx context.Context, s Stream[T], sinks ...Sink[T]) error

Sinks

func Discard[T any]() Sink[T]
func ToSlice[T any](dst *[]T) Sink[T]
func ToChan[T any](ch chan<- T, opts ...ChanOption) Sink[T]
func ToFunc[T any](fn func(context.Context, T) error) Sink[T]
func ToWriter[T ~[]byte | ~string](w io.Writer) Sink[T]
func ToCSV(w io.Writer, opts ...CSVOption) Sink[[]string]

Option Types & Constructors

type ParOption func(*parConfig)
func Workers(n int) ParOption
func Ordered() ParOption
func BufferSize(n int) ParOption

type BatchOption func(*batchConfig)
func MaxSize(n int) BatchOption
func MaxWait(d time.Duration) BatchOption

type BufferOption func(*bufferConfig)
func Block() BufferOption
func DropOldest() BufferOption
func DropNewest() BufferOption

type RateOption func(*rateConfig)
func Burst(n int) RateOption

type RetryOption func(*retryConfig)
func Multiplier(m float64) RetryOption
func MaxDelay(d time.Duration) RetryOption
func Jitter(fraction float64) RetryOption
func RetryIf(pred func(error) bool) RetryOption

type ChanOption func(*chanConfig)
func CloseOnDone() ChanOption

type CSVOption func(*csvConfig)
func Comma(r rune) CSVOption
func SkipHeader() CSVOption