iters

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2025 License: MIT Imports: 3 Imported by: 7

README

iters

Generic iterators library in Go.

Apart from providing a base interface for iterators:

type Iter[T any] interface {
	// Next returns the next item.
	// It returns io.EOF if there are no more items left.
	Next() (T, error)
	Close()
}

It also provides a more typical iterator interface for integrating with various APIs:

type PageIter[T any] interface {
	// NextPage returns the next page of items.
	// It returns io.EOF if there are no more items left.
	NextPage(ctx context.Context) ([]T, error)
	Close()
}

The library allows you to convert between these iterator interfaces, and provides additional tooling like filtering, converting values, etc.

For Go 1.23+ conversion to/from iter.Seq is available.

One other useful feature is encoding/decoding iterators to/from JSON streams (or any other encoder/decoder).

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](it Iter[T]) ([]T, error)

All returns all Iter results as a slice.

func AllCtx

func AllCtx[T any](ctx context.Context, it IterCtx[T]) ([]T, error)

AllCtx returns all IterCtx results as a slice.

func AllPages

func AllPages[T any](ctx context.Context, it PageIter[T]) ([]T, error)

AllPages returns all PageIter results as a slice.

func AsSeq added in v1.1.0

func AsSeq[T any](it Iter[T]) iter.Seq[T]

AsSeq returns a sequence of results. It stops if an error is encountered.

func AsSeqErr added in v1.1.0

func AsSeqErr[T any](it Iter[T]) iter.Seq2[T, error]

AsSeqErr returns a sequence of results and non-EOF errors.

func Encode

func Encode[T any](enc Encoder, it Iter[T]) error

Encode an iterator.

Types

type Decoder

type Decoder interface {
	Decode(v any) error
}

type Encoder

type Encoder interface {
	Encode(v any) error
}

type Iter

type Iter[T any] interface {
	// Next returns the next item.
	// It returns io.EOF if there are no more items left.
	Next() (T, error)
	// Close the iterator.
	Close()
}

Iter is a generic interface for the iterator.

Example:

defer it.Close()
for {
	v, err := it.Next()
	if err == io.EOF {
		break
	} else if err != nil {
		panic(err)
	}
	fmt.Println(v)
}

func Decode

func Decode[T any](dec Decoder) Iter[T]

Decode an iterator.

It will use type T as a destination type, thus it must not be an interface.

func Filter

func Filter[T any](it Iter[T], filter func(v T) bool) Iter[T]

Filter iterator results with a function. If function returns false, result is skipped.

func FromRows added in v1.2.0

func FromRows[T any](rows RowsScanner, scan func() (T, error)) Iter[T]

FromRows converts sql.Rows to Iter.

func Join

func Join[T any](sub ...Iter[T]) Iter[T]

Join iterators by listing elements sequentially from each iterator in order. If any of the iterators fail, an error is returned.

func Limit added in v1.1.0

func Limit[T any](it Iter[T], limit int) Iter[T]

Limit the number of results returned by the iterator. It returns all results if limit < 0.

func Map

func Map[T1 any, T2 any](it Iter[T1], conv func(v T1) (T2, error)) Iter[T2]

Map iterator items to a different type.

func MultiIter

func MultiIter[T any](strict bool, sub ...Iter[T]) Iter[T]

MultiIter creates a new iterator that alternates between results from sub iterators.

If strict is set to true, the iterator will fail on the first non io.EOF error. If it's false, the iterator will return the last error when all iterators are done (or fail).

func PagesAsIter

func PagesAsIter[T any](ctx context.Context, it PageIter[T]) Iter[T]

PagesAsIter takes a PageIter and converts it to an Iter.

func Seq added in v1.1.0

func Seq[T any](it iter.Seq[T]) Iter[T]

Seq creates an iterator from a sequence.

func SeqErr added in v1.1.0

func SeqErr[T any](it iter.Seq2[T, error]) Iter[T]

SeqErr creates an iterator from a sequence of values and errors.

type IterCtx

type IterCtx[T any] interface {
	// NextCtx returns the next item.
	// It returns io.EOF if there are no more items left.
	NextCtx(ctx context.Context) (T, error)
	// Close the iterator.
	Close()
}

IterCtx is a generic interface for the iterator with a context.

Example:

defer it.Close()
for {
	v, err := it.NextCtx(ctx)
	if err == io.EOF {
		break
	} else if err != nil {
		panic(err)
	}
	fmt.Println(v)
}

func FilterCtx

func FilterCtx[T any](it IterCtx[T], filter func(v T) bool) IterCtx[T]

FilterCtx filters iterator results with a function. If function returns false, result is skipped.

func JoinCtx

func JoinCtx[T any](sub ...IterCtx[T]) IterCtx[T]

JoinCtx iterators by listing elements sequentially from each iterator in order. If any of the iterators fail, an error is returned.

func MapCtx

func MapCtx[T1 any, T2 any](it IterCtx[T1], conv func(ctx context.Context, v T1) (T2, error)) IterCtx[T2]

MapCtx maps iterator items to a different type.

func MultiIterCtx

func MultiIterCtx[T any](strict bool, sub ...IterCtx[T]) IterCtx[T]

MultiIterCtx creates a new iterator that alternates between results from sub iterators.

If strict is set to true, the iterator will fail on the first non io.EOF error. If it's false, the iterator will return the last error when all iterators are done (or fail).

func PagesAsIterCtx

func PagesAsIterCtx[T any](it PageIter[T]) IterCtx[T]

PagesAsIterCtx takes a PageIter and converts it to an IterCtx.

type PageIter

type PageIter[T any] interface {
	// NextPage returns the next page of items.
	// It returns io.EOF if there are no more items left.
	NextPage(ctx context.Context) ([]T, error)
	// Close the iterator.
	Close()
}

PageIter is an iterator that iterates over pages of items.

func FilterPage

func FilterPage[T any](it PageIter[T], filter func(v T) bool) PageIter[T]

FilterPage filters page iterator results with a function. If function returns false, result is skipped.

func IterWithPage

func IterWithPage[T any](it Iter[T], page int) PageIter[T]

IterWithPage takes an Iter and converts it to an PageIter with a specific page size.

func IterWithPageCtx

func IterWithPageCtx[T any](it IterCtx[T], page int) PageIter[T]

IterWithPageCtx takes an IterCtx and converts it to an PageIter with a specific page size.

func JoinPages

func JoinPages[T any](sub ...PageIter[T]) PageIter[T]

JoinPages joins iterators by listing pages sequentially from each iterator in order. If any of the iterators fail, an error is returned.

func MapPage

func MapPage[T1 any, T2 any](it PageIter[T1], conv func(ctx context.Context, v T1) (T2, error)) PageIter[T2]

MapPage maps page iterator items to a different type.

func MultiPageIter

func MultiPageIter[T any](strict bool, sub ...PageIter[T]) PageIter[T]

MultiPageIter creates a new iterator that alternates between pages from sub iterators.

If strict is set to true, the iterator will fail on the first non io.EOF error. If it's false, the iterator will return the last error when all iterators are done (or fail).

func PageSlice

func PageSlice[T any](s [][]T) PageIter[T]

PageSlice converts a slice of pages into a PageIter.

func SeqErrPages added in v1.1.0

func SeqErrPages[T any](it iter.Seq2[[]T, error]) PageIter[T]

SeqErrPages creates an iterator from a sequence of pages and errors.

func SeqPages added in v1.1.0

func SeqPages[T any](it iter.Seq[[]T]) PageIter[T]

SeqPages creates an iterator from a sequence of pages.

type PagedIter

type PagedIter[T any] interface {
	Iter[T]
	IterCtx[T]
	PageIter[T]
}

PagedIter combines Iter and PageIter.

func Error added in v1.2.0

func Error[T any](err error) PagedIter[T]

Error creates Iter that always return an error.

func ErrorCtx added in v1.2.0

func ErrorCtx[T any](err error) PagedIter[T]

ErrorCtx creates IterCtx that always return an error.

func Slice

func Slice[T any](s []T) PagedIter[T]

Slice converts a slice to a PagedIter.

When used as a PageIter, it returns all items as a single page. See PageSlice or IterWithPage for controlling the page size.

type RowsScanner added in v1.2.0

type RowsScanner interface {
	Next() bool
	Err() error
	Close() error
}

RowsScanner is a minimal interface that sql.Rows implements.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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