itertools

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2025 License: MIT Imports: 2 Imported by: 0

README

go-itertools

Common utilities to work with Go 1.23's iterators, inspired by Python's and Rust's itertools.

Documentation

Overview

Package itertools implements common utilities to work with Go 1.23's iterators.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[V any](seq iter.Seq[V], p func(V) bool) bool

All reports whether all values yielded by seq pass p. All is short-circuiting, i.e. it will stop when it reaches a value that does not pass p.

func Any

func Any[V any](seq iter.Seq[V], p func(V) bool) bool

Any reports whether any value yielded by seq passes p. Any is short-circuiting, i.e. it will stop when it reaches a value that passes p.

func Chain

func Chain[V any](seq1, seq2 iter.Seq[V]) iter.Seq[V]

Chain returns an iterator that will first yield all the values from seq1, then all the values from seq2.

func ChunkBy added in v0.2.0

func ChunkBy[V any, K comparable](seq iter.Seq[V], key func(V) K) iter.Seq[iter.Seq[V]]

ChunkBy returns an iterator that groups values from seq according to key and yields those groups. Consecutive elements that map to the same key are assigned to the same group.

func Chunks added in v0.2.0

func Chunks[V any](seq iter.Seq[V], s uint) iter.Seq[iter.Seq[V]]

Chunks returns an iterator that chunks values from seq into groups of size s.

func Cycle

func Cycle[V any](seq iter.Seq[V]) iter.Seq[V]

Cycle returns an iterator that cycles through seq indefinitely. Values from seq are progressively accumulated into a slice during the first cycle, and reused for the next cycles.

func Drop

func Drop[V any](seq iter.Seq[V], n uint) iter.Seq[V]

Drop returns an iterator that will drop the n first values from seq.

func DropWhile

func DropWhile[V any](seq iter.Seq[V], p func(V) bool) iter.Seq[V]

DropWhile returns an iterator that will drop values from seq as long as they pass p. The iterator yields the remaining values when it encounters the first value that does not pass p.

func Filter

func Filter[V any](seq iter.Seq[V], p func(V) bool) iter.Seq[V]

Filter returns an iterator that will yield values from seq only if they pass p.

func Flatten

func Flatten[V any](seq iter.Seq[iter.Seq[V]]) iter.Seq[V]

Flatten returns an iterator that yields each value from a nested iterator.

func FromMap added in v0.2.0

func FromMap[K comparable, V any](m map[K]V) iter.Seq2[K, V]

FromMap returns an iterator yielding all the values from m.

func FromSlice

func FromSlice[V any](vs []V) iter.Seq[V]

FromSlice returns an iterator yielding all the values from vs.

func InterleaveLongest added in v0.2.0

func InterleaveLongest[V any](seq1, seq2 iter.Seq[V]) iter.Seq[V]

InterleaveLongest returns an iterator that will yield values from seq1 and seq2 alternatively, starting with seq1. The iterator stops after both seq1 and seq2 are exhausted.

func InterleaveShortest added in v0.2.0

func InterleaveShortest[V any](seq1, seq2 iter.Seq[V]) iter.Seq[V]

InterleaveShortest returns an iterator that will yield values from seq1 and seq2 alternatively, starting with seq1. The iterator stops after the iterator whose turn it is to produce a value is exhausted.

func IsSorted added in v0.2.0

func IsSorted[V cmp.Ordered](seq iter.Seq[V]) bool

IsSorted reports whether values yielded by seq are sorted in ascending order.

func IsSortedFunc added in v0.2.0

func IsSortedFunc[V any](seq iter.Seq[V], cmp func(V, V) int) bool

IsSortedFunc reports whether values yielded by seq are sorted in ascending order, comparing them using cmp.

func Map

func Map[V any, W any](seq iter.Seq[V], f func(V) W) iter.Seq[W]

Map returns an iterator that will yield values from seq after transforming them using f.

func MapFromSeq2 added in v0.2.0

func MapFromSeq2[V any, W any, X any](seq iter.Seq2[V, W], f func(V, W) X) iter.Seq[X]

MapFromSeq2 returns an iterator that will yield values from seq after transforming them using f. It is a specialization of Map for when seq is an iter.Seq2 iterator.

func MapToSeq2 added in v0.2.0

func MapToSeq2[V any, W any, X any](seq iter.Seq[V], f func(V) (W, X)) iter.Seq2[W, X]

MapToSeq2 returns an iterator that will yield values from seq after transforming them using f. It is a specialization of Map for when the returned iterator is an iter.Seq2 iterator.

func Max

func Max[V cmp.Ordered](seq iter.Seq[V]) (V, bool)

Max returns the minimum value yielded by seq. If no values are yielded by seq, a zero-value is returned and the second return value is false. If there is more than one minimal element according to the cmp function, Max returns the first one.

func MaxFunc

func MaxFunc[V any](seq iter.Seq[V], cmp func(V, V) int) (V, bool)

MaxFunc returns the minimum value yielded by seq, comparing values using cmp. If no values are yielded by seq, a zero-value is returned and the second return value is false. If there is more than one minimal element according to the cmp function, MaxFunc returns the first one.

func Min

func Min[V cmp.Ordered](seq iter.Seq[V]) (V, bool)

Min returns the minimum value yielded by seq. If no values are yielded by seq, a zero-value is returned and the second return value is false. If there is more than one minimal element according to the cmp function, Min returns the first one.

func MinFunc

func MinFunc[V any](seq iter.Seq[V], cmp func(V, V) int) (V, bool)

MinFunc returns the minimum value yielded by seq, comparing values using cmp. If no values are yielded by seq, a zero-value is returned and the second return value is false. If there is more than one minimal element according to the cmp function, MinFunc returns the first one.

func None

func None[V any](seq iter.Seq[V], p func(V) bool) bool

None reports whether no value yielded by seq passes p. None is short-circuiting, i.e. it will stop when it reaches a value that passes p.

func Reduce added in v0.2.0

func Reduce[V any, W any](seq iter.Seq[V], f func(W, V) W, init W) W

Reduce reduces the values yielded by seq to a single one by repeatedly applying f.

func Repeat

func Repeat[V any](v V) iter.Seq[V]

Repeat returns an iterator that will indefinitely yield v.

func RepeatN

func RepeatN[V any](v V, n uint) iter.Seq[V]

RepeatN works like Repeat, but returns an iterator that stops after yielding n values.

func ReverseSlice added in v0.2.0

func ReverseSlice[V any](vs []V) iter.Seq[V]

ReverseSlice returns an iterator that will yield values from vs in reversed order/

func Take

func Take[V any](seq iter.Seq[V], n uint) iter.Seq[V]

Take returns an iterator that will yield the n first values from seq.

func TakeWhile

func TakeWhile[V any](seq iter.Seq[V], p func(V) bool) iter.Seq[V]

TakeWhile returns an iterator that will yield values from seq as long as they pass p. The iterator stops when it encounters a value that does not pass p.

func WithFunc

func WithFunc[V any](f func() V) iter.Seq[V]

WithFunc returns an iterator yielding values obtained by indefinitely calling f.

func ZipShortest added in v0.2.0

func ZipShortest[V, W any](seq1 iter.Seq[V], seq2 iter.Seq[W]) iter.Seq2[V, W]

ZipShortest returns an iterator that will yield values from seq1 and seq2 simultaneously. The iterator stops after either seq1 or seq2 stops.

Types

This section is empty.

Jump to

Keyboard shortcuts

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