iter

package
v1.8.3 Latest Latest
Warning

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

Go to latest
Published: May 26, 2023 License: Apache-2.0, MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadAll

func ReadAll[T any](iter Iter[T]) []T

Types

type Iter

type Iter[T any] interface {
	// Next sets the iterator to the next value, returning true if an attempt was made to get the next value.
	Next() bool
	Val() T
	// Close closes the iterator and any underlying resources. Failure to close an iterator may result in resource leakage (goroutines, FDs, conns, etc.).
	Close() error
}

Iter is an iterator of arbitrary values. Iterators are generally not goroutine-safe, to make them safe just read from them into a channel. For our use cases, these usually have a single reader. This motivates iterators instead of channels, since the overhead of goroutines+channels has a significant performance cost. Using an iterator, you can read results directly without necessarily involving the Go scheduler.

There are a lot of options for an iterator interface, this one was picked for ease-of-use and for highest probability of consumers using it correctly. E.g. because there is a separate method for the value, it's easier to use in a loop but harder to implement.

Hopefully in the future, Go will include an iterator in the language and we can remove this.

func ToResultIter

func ToResultIter[T any](iter Iter[T]) Iter[Result[T]]

ToResultIter returns an iterator that wraps each value in a Result.

type JSONIter

type JSONIter[T any] struct {
	Decoder *json.Decoder
	Reader  io.Reader
	// contains filtered or unexported fields
}

JSONIter iterates over whitespace-delimited JSON values of a byte stream. This closes the reader if it is a closer, to faciliate easy reading of HTTP responses.

func FromReaderJSON

func FromReaderJSON[T any](r io.Reader) *JSONIter[T]

FromReaderJSON returns an iterator over the given reader that reads whitespace-delimited JSON values.

func (*JSONIter[T]) Close

func (j *JSONIter[T]) Close() error

func (*JSONIter[T]) Next

func (j *JSONIter[T]) Next() bool

func (*JSONIter[T]) Val

func (j *JSONIter[T]) Val() Result[T]

type MapIter

type MapIter[T any, U any] struct {
	// contains filtered or unexported fields
}

func Map

func Map[T any, U any](iter Iter[T], f func(t T) U) *MapIter[T, U]

Map invokes f on each element of iter.

func (*MapIter[T, U]) Close

func (m *MapIter[T, U]) Close() error

func (*MapIter[T, U]) Next

func (m *MapIter[T, U]) Next() bool

func (*MapIter[T, U]) Val

func (m *MapIter[T, U]) Val() U

type Result

type Result[T any] struct {
	Val T
	Err error
}

type ResultIter

type ResultIter[T any] interface{ Iter[Result[T]] }

type SliceIter

type SliceIter[T any] struct {
	Slice []T
	// contains filtered or unexported fields
}

func FromSlice

func FromSlice[T any](s []T) *SliceIter[T]

FromSlice returns an iterator over the given slice.

func (*SliceIter[T]) Close

func (s *SliceIter[T]) Close() error

func (*SliceIter[T]) Next

func (s *SliceIter[T]) Next() bool

func (*SliceIter[T]) Val

func (s *SliceIter[T]) Val() T

Jump to

Keyboard shortcuts

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