stream

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2022 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsReceiver

func AsReceiver[T any](a []chan T) []<-chan T

AsReceiver downcasts a slice of bidirectional channels to receive-only channels. The returned slice points to the same underlying array: modify it at your peril!

func AsSender

func AsSender[T any](a []chan T) []chan<- T

AsSender downcasts a slice of bidirectional channels to send-only channels. The returned slice points to the same underlying array: modify it at your peril!

func ChanToSlice

func ChanToSlice[T any](ctx context.Context, ch <-chan T) ([]T, error)

ChanToSlice collects the results of ch into a slice, returning when the channel is closed or the context is done, whichever comes first. Termination: this terminates if the context does.

func ChanToSliceN

func ChanToSliceN[T any](ctx context.Context, ch <-chan T, n int) ([]T, error)

ChanToSliceN collects at most N elements of ch into a slice, returning under the following conditions: - the channel is closed - the context is done - N elements have been collected

func CombineIntoN

func CombineIntoN[T any](ctx context.Context, dst chan<- T, src ...<-chan T) (int, error)

CombineIntoN feeds N input channels into a single output channel.

func CombineN

func CombineN[T any](ctx context.Context, src ...<-chan T) <-chan T

func ForkJoin

func ForkJoin[T, R any](ctx context.Context, a []T, f func(context.Context, T) (R, error)) (r []R, e []error)

ForkJoin executes a function 'f' in parallel across a slice T. The following guarantees hold:

  • len(a) == len(r) == len(e)
  • r[i], err[i] = f(a[i])

`f` must be responsible for respecting the context's cancellation. ForkJoin terminates if and only if f(ctx, a[i]) does.

func Range

func Range[T constraints.Integer](n int) chan T

func SliceToChan

func SliceToChan[T any](a []T) <-chan T

SliceToChan converts a slice []T to a buffered channel of the same length.

func Tee

func Tee[T any](ctx context.Context, src <-chan T) (a, b <-chan T)

Tee immediately returns two channels, each of which outputs the identical input from src. That is, for every read from src, Tee writes a shallow copy to A and to B. Be careful with pointers. See TeeN for more than two copies.

func TeeN

func TeeN[T any](ctx context.Context, src <-chan T, n int) (copies []<-chan T)

TeeN is the generalized form of Tee. It returns N channels, each of which outputs the identical input from src. That is, for every read from src, Tee writes a shallow copy to A and to B. Be careful with pointers. See Tee for just two output channels.

func TeeTo

func TeeTo[T any](ctx context.Context, src <-chan T, L, R chan<- T) error

TeeTo is as Tee, but pipes to the provided channels instead of making it's own. It does NOT close the channel. Termination: this function terminates when L and R are drained or when the context is cancelled.

func TeeToN

func TeeToN[T any](ctx context.Context, src <-chan T, dst []chan<- T) (n int, err error)

TeeToN is as TeeN, but pipes to the provided channels instead of making it's own. Safety: this is considered to take ownership of the *slice* dst, which is not safe for use afterwards. For every read from src, TeeTooN writes a shallow copy to each channel in dst.

 Terminates when:
	- the context is cancelled:
	- src has been closed and every entry has been written to dst.

Safety:

Replacing dst[i] (with nil or with another channel) after a call to TeeToN is undefined behavior. Failing to drain any given chaanel may block the others. Be careful with pointers.

Types

type Result

type Result[T, R any] struct {
	In  T
	Out R
	Err error
}

type Results

type Results[T, R any] <-chan *Result[T, R]

Results is a read-only channel of the results of a mapping operation.

func StreamMap

func StreamMap[T, R any](ctx context.Context, in chan T, f func(context.Context, T) (R, error)) Results[T, R]

StreamMap executes "f" in parallel across the items piped to "in", creating a ResultStream. It will attempt to terminate early if the context is done, but "f" is responsible for respecting the context's cancellation, and the consumer(s) of the ResultStream must either consume the result entirely or drop it out of scope.

func (Results[T, R]) CollectN

func (rs Results[T, R]) CollectN(n int) (out []R, err error)

CollectN collects at most N elements from the result stream. Termination:

func (Results[T, R]) CollectNCtx

func (rs Results[T, R]) CollectNCtx(ctx context.Context, n int) (out []R, err error)

CollectNCtx elements from the ResultStream until one of these happens:

  • The ResultStream is closed.
  • N elements have been obtained.
  • The context has been cancelled.

Note that this allocates a slice of [R] of capacity N.

Directories

Path Synopsis
internal
generated
autogenerated by codegen/combine.
autogenerated by codegen/combine.

Jump to

Keyboard shortcuts

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