it

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2025 License: MIT Imports: 2 Imported by: 0

README

it

it is some helpers for working with iterators that I don't want to have to keep writing.

Documentation

Overview

package it provides some tools for working with iterators, inspired by Python's itertools.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(bs iter.Seq[bool]) bool

All is a specialised fold for iterators of bools that returns true iff all of the values yielded by the iterator are true.

func Batch added in v0.0.2

func Batch[A any](i iter.Seq[A], n int) iter.Seq[[]A]

Batch returns an iterator that yields batches of n consecutive values from the provided iterator. The last batch may be smaller. The yielded slice is only valid until the next value is yields (it is reused between batches).

func Chain

func Chain[A any](its ...iter.Seq[A]) iter.Seq[A]

Chain takes a number of iterators and returns a single iterator that yields of the values from all of the iterators in sequence, starting with the first argument, then the second and so on.

func CollectErr added in v0.0.4

func CollectErr[A any](i iter.Seq2[A, error]) ([]A, error)

CollectErr collects all of the A elements from the iterator, up until the first non-nil error. When a non-nil error is encountered it is immediately returned, along with with any values successfully retrieved up to that point. Note that any value returned alongside the error will _not_ be returned; the assumption is that the iterator either returns an error or a value and not both.

func Concat

func Concat[A any](its iter.Seq[iter.Seq[A]]) iter.Seq[A]

Concat is like chain, but accepts an iterator of iterators.

func Const

func Const[A any](a A) iter.Seq[A]

Const returns an iterator that continually yields the provided value, forever. Note that this is an infinite iterator, intended to be used with something like Zip or Take that will stop early.

func Enumerate added in v0.0.2

func Enumerate[A any](it iter.Seq[A]) iter.Seq2[int, A]

Enumerate returns an iterator that pairs each element in the provided sequence with its index in the sequence, starting from 0.

func Filter

func Filter[A any](it iter.Seq[A], p func(A) bool) iter.Seq[A]

Filter returns an iterator which yields only those values in it for which p returns true.

func Fold

func Fold[A, B any](it iter.Seq[A], z B, f func(A, B) B) B

Fold performs a left fold across the iterator using the provided combining function and initial value.

func Limit added in v0.0.2

func Limit[A any](i iter.Seq[A], n int) iter.Seq[A]

Limit returns a new iterator that yields the first n values from the provided iterator and then stops. If the parent iterator has fewer than n values, the returned child iterator will just stop when it runs out.

func Map

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

Map applies a function to every item in the iterator.

func Map1x2

func Map1x2[A, B, C any](as iter.Seq[A], f func(A) (B, C)) iter.Seq2[B, C]

Map1x2 maps an iter.Seq to an iter.Seq2 by applying the provided function to each item in turn.

func Map2x1

func Map2x1[A, B, C any](abs iter.Seq2[A, B], f func(A, B) C) iter.Seq[C]

Map2x1 maps an iter.Seq2 to an iter.Seq by applying the provided function to each pair of items in turn.

func Map2x2

func Map2x2[A, B, C, D any](abs iter.Seq2[A, B], f func(A, B) (C, D)) iter.Seq2[C, D]

Map2x2 maps an iter.Seq2 to an iter.Seq2 by applying the provided function to each pair of items in turn.

func Perm added in v0.0.3

func Perm[E any, S ~[]E](data S) iter.Seq[S]

Perm returns an iterator that yields all permutations of the provided slice. It shuffles the objects in place, and always yields the same slice, so care must be taken if re-using the yielded values.

func Take

func Take[A any](it iter.Seq[A], n int) iter.Seq[A]

Take returns an iterator that yields at most the first n elements of the provided iterator and then stops.

func TakeWhile

func TakeWhile[A any](it iter.Seq[A], p func(A) bool) iter.Seq[A]

TakeWhile returns an iterator that yields the (possibly empty) prefix of the provided iterator for which the given predicate returns true. The returned iterator finishes as soon as it yields a value for which p returns false.

func Unpair

func Unpair[A, B any](i iter.Seq[Pair[A, B]]) iter.Seq2[A, B]

Unpair is a convenience for turning an iter.Seq[Pair[A, B]] into an iter.Seq2[A, B].

func Zip

func Zip[A, B any](as iter.Seq[A], bs iter.Seq[B]) iter.Seq2[A, B]

Zip returns an iterator that iterates through a and b at the same time, yielding pairs of adjacent items. The returned iterator stops as soon as either as or bs runs out of items.

Types

type Pair

type Pair[A, B any] struct {
	A A
	B B
}

Pair is just a pair of two elements, for occasions where we need to do things like collect the values in an iter.Seq2.

func Collect2

func Collect2[A, B any](i iter.Seq2[A, B]) []Pair[A, B]

Collect2 is like slices.Collect, but works with iter.Seq2, returning all of the results as Pairs.

func NewPair

func NewPair[A, B any](a A, b B) Pair[A, B]

NewPair creates a new pair. It is useful to have this defined as a function, such as when implementing Collect2.

func (Pair[A, B]) Values

func (p Pair[A, B]) Values() (A, B)

Values returns the values of the pair.

Jump to

Keyboard shortcuts

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