iter

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package iter is a Go library for working with the golang iter package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Channel

func Channel[T any](ch <-chan T) iter.Seq[T]

Channel returns a sequence that yields values from a channel. If the channel is closed, the sequence is complete.

func Channel2

func Channel2[K, V any](ch <-chan lang.Tuple2[K, V]) iter.Seq2[K, V]

Channel2 returns a sequence that yields key/value pairs from a channel. If the channel is closed, the sequence is complete.

func Concat

func Concat[T any](seq ...iter.Seq[T]) iter.Seq[T]

Concat returns a sequence that concatenates the given sequences.

func Concat2

func Concat2[K, V any](seq ...iter.Seq2[K, V]) iter.Seq2[K, V]

Concat2 returns a sequence that concatenates the given sequences.

func Context

func Context[T any](ctx context.Context, s iter.Seq[T]) iter.Seq[T]

Context returns a new Seq that will yield elements from the input Seq until the context is done.

func Context2

func Context2[K, V any](ctx context.Context, s iter.Seq2[K, V]) iter.Seq2[K, V]

Context2 returns a new Seq2 that will yield elements from the input Seq2 until the context is done.

func Deferred

func Deferred[T any](f DeferredF[T]) iter.Seq[T]

Deferred returns a sequence that yields values from a function.

func Deferred2

func Deferred2[K, V any](f Deferred2F[K, V]) iter.Seq2[K, V]

Deferred2 returns a sequence that yields key/value pairs from a function.

func Empty

func Empty[T any]() iter.Seq[T]

Empty returns an empty sequence.

func Empty2

func Empty2[K, V any]() iter.Seq2[K, V]

Empty2 returns an empty sequence.

func Filter

func Filter[T any](m iter.Seq[T], f func(T) bool) iter.Seq[T]

Filter returns a new iterator that yields only the elements of the input iterator for which the predicate function returns true.

func Filter2

func Filter2[K, V any](m iter.Seq2[K, V], f func(K, V) bool) iter.Seq2[K, V]

Filter2 returns a new iterator that yields only the elements of the input iterator for which the predicate function returns true.

func FlatMap

func FlatMap[A, B any](s iter.Seq[A], f func(A) iter.Seq[B]) iter.Seq[iter.Seq[B]]

FlatMap returns a new Seq that will yield elements from the input Seq that are the result of applying the function to each element.

func FlatMapConcat

func FlatMapConcat[A, B any](s iter.Seq[A], f func(A) iter.Seq[B]) iter.Seq[B]

FlatMapConcat returns a new Seq that will yield elements from the input Seq that are the result of applying the function to each element and concatenating the results.

func Flatten

func Flatten[T any](s iter.Seq[iter.Seq[T]]) iter.Seq[T]

Flatten returns a new Seq that will yield elements from the nested input Seq.

func ForEach

func ForEach[T any](seq iter.Seq[T], f func(T))

ForEach applies the function f to each element of the sequence seq.

func GroupBy

func GroupBy[T any, K comparable](seq iter.Seq[T], key func(T) K) iter.Seq2[K, iter.Seq[T]]

GroupBy returns a sequence that groups elements from the given sequence by the given key function. The key function is used to determine the group of each element. The resulting sequence yields key-value pairs where the key is the group and the value is a sequence of elements in that group. This function must consume the entire input sequence to group the elements so it's not efficient for large sequences.

func Len

func Len[T any](seq iter.Seq[T]) int

Len returns the number of elements in the sequence. This requires iterating over the entire sequence so it is not efficient for large sequences.

func Len2

func Len2[K, V any](seq iter.Seq2[K, V]) int

Len2 returns the number of elements in the sequence. This requires iterating over the entire sequence so it is not efficient for large sequences.

func Map

func Map[A, B any](s iter.Seq[A], f func(A) B) iter.Seq[B]

Map applies a function to each element of a sequence and returns a new sequence with the results.

func Map2

func Map2[A, B, A1, B1 any](s iter.Seq2[A, B], f func(A, B) (A1, B1)) iter.Seq2[A1, B1]

Map2 applies a function to each element of a sequence and returns a new sequence with the results.

func Range

func Range(start, end int) iter.Seq[int]

Range returns a sequence of integers from start to end.

func Range2

func Range2(start, end int) iter.Seq2[int, struct{}]

Range2 returns a sequence of integers from start to end.

func RecoverIf

func RecoverIf[T any](s iter.Seq[lang.Either[error, T]], p Predicate[error], rec RecoverFunc[T]) iter.Seq[lang.Either[error, T]]

RecoverIf applies a function to errors in a sequence and returns a new sequence with the results.

func RecoverIf2

func RecoverIf2[T any](s iter.Seq2[T, error], p Predicate[error], rec RecoverFunc2[T]) iter.Seq2[T, error]

RecoverIf2 applies a function to errors in a sequence and returns a new sequence with the results.

func Rows

func Rows[T any](rows RowLike, scanner Scanner[T]) iter.Seq[lang.Either[error, T]]

Rows returns a sequence of values from a database RowsLike instance. If an error occurs reading the row it is returned as a Left value in the sequence. If the row is read successfully, the value is returned as a Right value.

func Rows2

func Rows2[T any](rows RowLike, scanner Scanner[T]) iter.Seq2[T, error]

Rows2 returns a sequence of values from a database RowsLike instance. If an error occurs reading the row it is returned as the second yield argument. If the row is read successfully, the value is returned as the first yield argument.

func Single

func Single[T any](value T) iter.Seq[T]

Single returns a sequence that yields a single value.

func Single2

func Single2[K, V any](key K, value V) iter.Seq2[K, V]

Single2 returns a sequence that yields a single key/value pair.

func Stream

func Stream[T any](stream StreamLike[T]) iter.Seq[lang.Either[error, T]]

Stream converts a StreamLike into an iter.Seq that can be used with the iter package. If the StreamLike returns an error, the iter.Seq will return an either.Left with that error. Otherwise, the iter.Seq will return an either.Right with the value.

func Stream2

func Stream2[T any](stream StreamLike[T]) iter.Seq2[T, error]

Stream2 converts a StreamLike into an iter.Seq2 that can be used with the iter package. If the StreamLike returns an error, the iter.Seq2 will return that error.

func Throttle

func Throttle[T any](seq iter.Seq[T], d time.Duration) iter.Seq[T]

Throttle returns a sequence that yields elements from the given sequence at a maximum rate of one element per duration d.

func Throttle2

func Throttle2[K, V any](seq iter.Seq2[K, V], d time.Duration) iter.Seq2[K, V]

Throttle2 returns a sequence that yields elements from the given sequence at a maximum rate of one element per duration d.

func ToSeq2

func ToSeq2[K, V any](it iter.Seq[K]) iter.Seq2[K, V]

ToSeq2 converts a Seq into a Seq2. The second type argument is ignored and will always be nil.

Types

type Deferred2F

type Deferred2F[K, V any] func() (lang.Tuple2[K, V], bool)

Deferred2F is a function that returns a tuple and a boolean indicating if the sequence is complete.

type DeferredF

type DeferredF[T any] func() (T, bool)

DeferredF is a function that returns a value and a boolean indicating if the sequence is complete.

type Predicate

type Predicate[T any] func(T) bool

Predicate is a function that returns true if the given value satisfies a condition.

type RecoverFunc

type RecoverFunc[T any] func(error) iter.Seq[lang.Either[error, T]]

RecoverFunc is a function that returns a sequence of values given an error.

type RecoverFunc2

type RecoverFunc2[T any] func(error) iter.Seq2[T, error]

RecoverFunc2 is a function that returns a sequence of values given an error.

type RowLike

type RowLike interface {
	// Next returns true if there are more rows to iterate over.
	Next() bool
	// Close closes the sql.Rows.
	Close()
	// Err returns the error encountered by the sql.Rows.
	Err() error
	// Scan scans the values from the sql.Rows into the given destination.
	Scan(dest ...any) error
}

RowLike is an interface for sql.Rows. It is used to allow for easier testing.

type Scanner

type Scanner[T any] func(func(dest ...any) error) (T, error)

Scanner is a function type for scanning values from database sql.Rows.

type StreamLike

type StreamLike[T any] interface {
	Recv() (T, error)
}

StreamLike is an interface that matches the gRPC Stream interface for receiving messages. This is existing to avoid dependencies on gRPC yet still be able to implement an Iterator that can be used with gRPC.

Jump to

Keyboard shortcuts

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