iterx

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: MIT Imports: 6 Imported by: 1

Documentation

Overview

Package iterx provides convenience functions for iterators.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CSVFile

func CSVFile(file string, fn func(*csv.Reader)) iter.Seq2[[]string, error]

CSVFile iterates over CSV entries from a file. fn is an optional function for modifying the CSV parser, for example for changing the delimiter.

func CSVReader

func CSVReader(r io.Reader, fn func(*csv.Reader)) iter.Seq2[[]string, error]

CSVReader iterates over CSV entries from a reader. fn is an optional function for modifying the CSV parser, for example for changing the delimiter.

func CollectErr added in v1.1.0

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

CollectErr collects the given T's in a slice until the error is non-nil.

func ISlice

func ISlice[T any](s []T) iter.Seq2[int, T]

ISlice returns an iterator over the slice values and their indices, like in a range expression.

func Limit

func Limit[T any](it iter.Seq[T], n int) iter.Seq[T]

Limit returns an iterator that stops after n elements, if the underlying iterator does not stop before.

func Limit2

func Limit2[T any, S any](it iter.Seq2[T, S], n int) iter.Seq2[T, S]

Limit2 returns an iterator that stops after n elements, if the underlying iterator does not stop before.

func LinesFile

func LinesFile(file string) iter.Seq2[string, error]

LinesFile iterates over text lines from a reader.

func LinesReader

func LinesReader(r io.Reader) iter.Seq2[string, error]

LinesReader iterates over text lines from a reader.

func Skip

func Skip[T any](it iter.Seq[T], n int) iter.Seq[T]

Skip returns an iterator without the first n elements.

func Skip2

func Skip2[T any, S any](it iter.Seq2[T, S], n int) iter.Seq2[T, S]

Skip2 returns an iterator without the first n elements.

func Slice

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

Slice returns an iterator over the slice values.

Types

type Unreader added in v0.6.0

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

An Unreader wraps an iterator and adds an unread function.

func NewUnreader added in v0.6.0

func NewUnreader[T any](seq iter.Seq[T]) *Unreader[T]

NewUnreader returns an Unreader with seq as its underlying iterator.

func (*Unreader[T]) GroupBy added in v0.6.0

func (r *Unreader[T]) GroupBy(sameGroup func(old T, nu T) bool,
) iter.Seq[iter.Seq[T]]

GroupBy returns an iterator over groups, where each group is an iterator that yields elements as long as the elements have sameGroup==true with the first element in the group.

func (*Unreader[T]) Read added in v0.6.0

func (r *Unreader[T]) Read() (T, bool)

Read returns the result of calling the underlying reader, or the last unread element.

func (*Unreader[T]) Unread added in v0.6.0

func (r *Unreader[T]) Unread()

Unread makes the next call to Read return the last element and a nil error. Can be called up to once per call to Read.

func (*Unreader[T]) Until added in v0.6.0

func (r *Unreader[T]) Until(stop func(T) bool) iter.Seq[T]

Until calls Read until stop returns true.

type Unreader2 added in v0.6.0

type Unreader2[T, S any] struct {
	// contains filtered or unexported fields
}

An Unreader2 wraps an iterator and adds an unread function.

func NewUnreader2 added in v0.6.0

func NewUnreader2[T, S any](seq iter.Seq2[T, S]) *Unreader2[T, S]

NewUnreader2 returns an Unreader2 with seq as its underlying iterator.

func (*Unreader2[T, S]) GroupBy added in v0.6.0

func (r *Unreader2[T, S]) GroupBy(
	sameGroup func(oldT T, oldS S, newT T, newS S) bool,
) iter.Seq[iter.Seq2[T, S]]

GroupBy returns an iterator over groups, where each group is an iterator that yields elements as long as the elements have sameGroup==true with the first element in the group.

func (*Unreader2[T, S]) Read added in v0.6.0

func (r *Unreader2[T, S]) Read() (T, S, bool)

Read returns the result of calling the underlying reader, or the last unread element.

func (*Unreader2[T, S]) Unread added in v0.6.0

func (r *Unreader2[T, S]) Unread()

Unread makes the next call to Read return the last element and a nil error. Can be called up to once per call to Read.

func (*Unreader2[T, S]) Until added in v0.6.0

func (r *Unreader2[T, S]) Until(stop func(T, S) bool) iter.Seq2[T, S]

Until calls Read until stop returns true.

Jump to

Keyboard shortcuts

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