iterhelper

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2025 License: MIT Imports: 10 Imported by: 1

README

iterhelper

Go Reference

Helpers for Go's iter package.

Documentation

Overview

Package iterhelper contains iter package helpers.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOddValues   = errors.New("odd number of values")
	ErrNilAction   = errors.New("nil action")
	ErrNilEqual    = errors.New("nil equal")
	ErrNilSec      = errors.New("nil Sec")
	ErrNilSec2     = errors.New("nil Sec2")
	ErrNilSelector = errors.New("nil selector")
)
View Source
var DefaultFormat = Format{
	LeftRim:          "",
	RightRim:         "",
	ElementSeparator: " ",
	LeftEdge:         "[",
	RightEdge:        "]",
	ValueSeparator:   ":",
}

DefaultFormat represents default formatting parameters used by StringDef and StringDef2. Assign desired values, if needed.

Functions

func ChanAll added in v0.5.0

func ChanAll[E any](c <-chan E) iter.Seq[E]

ChanAll returns an iterator over the elements of the channel. If 'c' is nil, iterator over the empty sequence is returned.

func ChanAll2 added in v0.5.0

func ChanAll2[E any](c <-chan E) iter.Seq2[int, E]

ChanAll2 returns an iterator over index-element pairs of the channel. If 'c' is nil, iterator over the empty sequence of pairs is returned.

func Collect2 added in v0.8.0

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

Collect2 returns a slice of values collected from the iterator. Each pair of values yielded by the iterator results in two values in the slice. If 'seq2' is nil, nil is returned.

func Collect2Tuple added in v0.9.0

func Collect2Tuple[K, V any](seq2 iter.Seq2[K, V]) []generichelper.Tuple2[K, V]

Collect2Tuple returns a slice of tuples of values collected from the iterator. If 'seq2' is nil, nil is returned.

func Empty added in v0.2.0

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

Empty returns an iterator over an empty sequence of values.

func Empty2 added in v0.2.0

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

Empty2 returns an iterator over an empty sequence of pairs of values.

func Equal added in v0.6.0

func Equal[V any](first, second iter.Seq[V]) (bool, error)

Equal determines whether two iterators yield the equal sequences by comparing their elements using generichelper.DeepEqual.

func Equal2 added in v0.6.0

func Equal2[K, V any](first, second iter.Seq2[K, V]) (bool, error)

Equal2 determines whether two iterators yield the equal sequences by comparing their elements using reflect.DeepEqual.

func EqualEq added in v0.6.0

func EqualEq[V any](first, second iter.Seq[V], equal func(V, V) bool) (bool, error)

EqualEq determines whether two iterators yield the equal sequences by comparing their elements using a specified function.

func EqualEq2 added in v0.6.0

func EqualEq2[K, V any](first, second iter.Seq2[K, V],
	equal func(k1 K, v1 V, k2 K, v2 V) bool) (bool, error)

EqualEq2 determines whether two iterators yield the equal sequences by comparing their elements using a specified function.

func ErrWrongType added in v0.7.0

func ErrWrongType(got, want any) error

func ForEach added in v0.2.0

func ForEach[V any](ctx context.Context, seq iter.Seq[V], action func(V) error) error

ForEach sequentially performs a specified 'action' on each value yielded by the iterator. If 'ctx' is canceled or 'action' returns a non-nil error, the operation is stopped and corresponding error is returned.

func ForEach2 added in v0.6.0

func ForEach2[K, V any](ctx context.Context, seq2 iter.Seq2[K, V], action func(K, V) error) error

ForEach2 sequentially performs a specified 'action' on each pair of values yielded by the iterator. If 'ctx' is canceled or 'action' returns a non-nil error, the operation is stopped and corresponding error is returned.

func ForEachConcurrent added in v0.2.0

func ForEachConcurrent[V any](ctx context.Context, seq iter.Seq[V], action func(V) error) error

ForEachConcurrent concurrently performs a specified 'action' on each value yielded by the iterator. If 'ctx' is canceled or 'action' returns a non-nil error, the operation is stopped and corresponding error is returned.

func ForEachConcurrent2 added in v0.6.0

func ForEachConcurrent2[K, V any](ctx context.Context, seq2 iter.Seq2[K, V], action func(K, V) error) error

ForEachConcurrent2 concurrently performs a specified 'action' on each pair of values yielded by the iterator. If 'ctx' is canceled or 'action' returns a non-nil error, the operation is stopped and corresponding error is returned.

func Seq2Seq added in v0.3.0

func Seq2Seq[K, V, V2 any](seq2 iter.Seq2[K, V], selector func(K, V) V2) (iter.Seq[V2], error)

Seq2Seq converts iter.Seq2 to iter.Seq.

func Seq2SeqK added in v0.3.0

func Seq2SeqK[K, V any](seq2 iter.Seq2[K, V]) (iter.Seq[K], error)

Seq2SeqK converts iter.Seq2 to iterator over keys of 'seq2'.

func Seq2SeqV added in v0.3.0

func Seq2SeqV[K, V any](seq2 iter.Seq2[K, V]) (iter.Seq[V], error)

Seq2SeqV converts iter.Seq2 to iterator over values of 'seq2'.

func SeqSeq2 added in v0.3.0

func SeqSeq2[V, K2, V2 any](seq iter.Seq[V], selector func(V) (K2, V2)) (iter.Seq2[K2, V2], error)

SeqSeq2 converts iter.Seq to iter.Seq2.

func StringDef added in v0.2.0

func StringDef[V any](seq iter.Seq[V]) string

StringDef returns string representation of a sequence of values by calling fmt.Sprint on each value yielded by the iterator and using default formatting parameters. If 'seq' is nil, empty string is returned.

func StringDef2 added in v0.2.0

func StringDef2[K, V any](seq2 iter.Seq2[K, V]) string

StringDef2 returns string representation of a sequence of pairs of values by calling fmt.Sprint on each value yielded by the iterator and using default formatting parameters. If 'seq2' is nil, empty string is returned.

func StringFmt added in v0.2.0

func StringFmt[V any](seq iter.Seq[V], format Format) string

StringFmt returns string representation of a sequence of values by calling fmt.Sprint on each value yielded by the iterator. If 'seq' is nil, empty string is returned.

func StringFmt2 added in v0.2.0

func StringFmt2[K, V any](seq2 iter.Seq2[K, V], format Format) string

StringFmt2 returns string representation of a sequence of pairs of values by calling fmt.Sprint on each value yielded by the iterator: If 'seq2' is nil, empty string is returned.

func StringSeq added in v0.2.0

func StringSeq[V any](seq iter.Seq[V]) (iter.Seq[string], error)

StringSeq converts an iterator to an iterator over strings by calling fmt.Sprint on each value yielded by the iterator.

func StringSlice added in v0.2.0

func StringSlice[V any](seq iter.Seq[V]) ([]string, error)

StringSlice returns a sequence of values yielded by the iterator as a slice of strings.

func Var added in v0.6.0

func Var[V any](vv ...V) iter.Seq[V]

Var returns an iterator over the variadic parameters/values.

func Var2 added in v0.6.0

func Var2[K, V any](vv ...any) (iter.Seq2[K, V], error)

Var2 returns an iterator over the variadic parameters/values. There must be an even number of parameters. Parameters must consist of pairs of values of type 'K' and 'V'.

func Var2Tuple added in v0.7.0

func Var2Tuple[K, V any](tt ...generichelper.Tuple2[K, V]) iter.Seq2[K, V]

Var2Tuple returns an iterator over the variadic tuples of values.

Types

type Format added in v0.9.0

type Format struct {
	// Left and right rims surround each element of a sequence.
	LeftRim, RightRim string
	// Element separator separates elements of a sequence.
	ElementSeparator string
	// Left and right edges surround the whole string.
	LeftEdge, RightEdge string
	// Value separator separates values in pair.
	ValueSeparator string
}

Format defines formatting parameters.

Jump to

Keyboard shortcuts

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