it

package module
v0.0.0-...-6a1fe6e Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2023 License: MIT Imports: 2 Imported by: 0

README

itertools for Go (1.22+)

Go Version Go Reference CI Go Report Card codecov

Go1.22 will support range over function and introduce the iter std package. (Behind the GOEXPERIMENT=rangefunc gate.)

See more at RangefuncExperiment.

This package provides a set of iterator functions borrowed from Python and Rust.

Some code are copied from Russ Cox's proposals 61898, 61899 and 61900.

Installation

go get github.com/j178/it

Usage

To use this package, you need to install at least Go 1.22(not released yet) and set GOEXPERIMENT=rangefunc.

go install golang.org/dl/gotip@latest
gotip download

GOEXPERIMENT=rangefunc gotip run main.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Accumulate

func Accumulate[Elem constraints.Integer | constraints.Float](seq iter.Seq[Elem]) iter.Seq[Elem]

Accumulate returns an iterator that returns accumulated sums.

func All

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

All returns true if all elements of seq satisfy f.

func Any

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

Any returns true if any element of seq satisfies f.

func Concat

func Concat[V any](seqs ...iter.Seq[V]) iter.Seq[V]

Concat returns an iterator over the concatenation of the sequences.

func Contains

func Contains[T comparable](seq iter.Seq[T], v T) bool

Contains returns true if seq contains v.

func ContainsBy

func ContainsBy[T any](seq iter.Seq[T], f func(T) bool) bool

ContainsBy returns true if seq contains an element that satisfies f.

func Count

func Count[T any](seq iter.Seq[T]) (cnt int)

Count returns the number of elements in seq.

func Count2

func Count2[K any, V any](seq iter.Seq2[K, V]) (cnt int)

Count2 returns the number of elements in seq.

func Cycle

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

Cycle returns an iterator returning elements from the iterable and saving a copy of each. When the iterable is exhausted, return elements from the saved copy. Repeats indefinitely.

func Enumerate

func Enumerate[V any](seq iter.Seq[V]) iter.Seq2[int, V]

Enumerate returns an iterator that returns (0, seq[0]), (1, seq[1]), (2, seq[2]), ...

func EnumerateByStep

func EnumerateByStep[V any](seq iter.Seq[V], start, step int) iter.Seq2[int, V]

EnumerateByStep returns an iterator that returns (start, seq[0]), (start+step, seq[1]), (start+2*step, seq[2]), ...

func Equal

func Equal[V comparable](x, y iter.Seq[V]) bool

Equal reports whether the two sequences are equal.

func Filter

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

Filter returns an iterator over seq that only includes the values v for which f(v) is true.

func Find

func Find[T any](seq iter.Seq[T], f func(T) bool) (T, int, bool)

Find searches for an element satisfying f in seq. If found, it returns the element and its index and true. Otherwise, it returns the zero value, -1, and false.

func FindOrElse

func FindOrElse[T any](seq iter.Seq[T], fallback T, f func(T) bool) T

FindOrElse searches for an element satisfying f in seq. If found, it returns the element. Otherwise, it returns fallback.

func Firsts

func Firsts[K any, V any](seq iter.Seq2[K, V]) iter.Seq[K]

Firsts returns a sequence of the first elements of a sequence of pairs.

func ForEach

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

ForEach calls f for each element of seq.

func FromChannel

func FromChannel[T any](ch <-chan T) iter.Seq[T]

FromChannel returns a sequence from a channel.

func IndexOf

func IndexOf[T comparable](seq iter.Seq[T], v T) int

IndexOf returns the first index of v in seq, or -1 if v is not present.

func Last

func Last[T any](seq iter.Seq[T]) (T, bool)

Last returns the last element of seq.

func Limit

func Limit[V any](seq iter.Seq[V], n int) iter.Seq[V]

Limit returns an iterator over seq that stops after n values.

func Limit2

func Limit2[K any, V any](seq iter.Seq2[K, V], n int) iter.Seq2[K, V]

Limit2 returns an iterator over seq that stops after n values.

func Map

func Map[In, Out any](f func(In) Out, seq iter.Seq[In]) iter.Seq[Out]

Map returns an iterator over f applied to seq.

func Max

func Max[T constraints.Ordered](seq iter.Seq[T]) (T, bool)

Max returns the maximum element of seq.

func MaxBy

func MaxBy[T any](seq iter.Seq[T], f func(T, T) bool) (T, bool)

MaxBy returns the maximum element of seq according to f.

func Min

func Min[T constraints.Ordered](seq iter.Seq[T]) (T, bool)

Min returns the minimum element of seq.

func Next

func Next[T any](seq iter.Seq[T]) (T, bool)

Next returns the first element of seq.

func Nth

func Nth[T any](seq iter.Seq[T], n int) (T, bool)

Nth returns the nth element of seq.

func Range

func Range(end int) iter.Seq[int]

Range returns a sequence of integers from 0 (inclusive) to end (exclusive).

func RangeByStep

func RangeByStep[T constraints.Integer | constraints.Float](start, end, step T) iter.Seq[T]

RangeByStep returns a sequence of integers from start (inclusive) to end (exclusive) by step. If step is zero, the sequence will be empty.

func RangeFrom

func RangeFrom[T constraints.Integer | constraints.Float](start, step T) iter.Seq[T]

RangeFrom returns a sequence of integers from start (inclusive) to infinity by step. If step is zero, the sequence will be empty.

func Repeat

func Repeat[Elem any](e Elem) iter.Seq[Elem]

Repeat returns an iterator that returns object indefinitely.

func RepeatN

func RepeatN[Elem any](e Elem, n int) iter.Seq[Elem]

RepeatN returns an iterator that returns object for the specified number of times.

func Seconds

func Seconds[K any, V any](seq iter.Seq2[K, V]) iter.Seq[V]

Seconds returns a sequence of the second elements of a sequence of pairs.

func Skip

func Skip[V any](seq iter.Seq[V], n int) iter.Seq[V]

Skip returns an iterator over seq that skips the first n values.

func ToChannel

func ToChannel[T any](seq iter.Seq[T]) <-chan T

func Zip

func Zip[V1, V2 any](x iter.Seq[V1], y iter.Seq[V2]) iter.Seq[Zipped[V1, V2]]

Zip returns an iterator that iterates x and y in parallel, yielding Zipped values of successive elements of x and y. If one sequence ends before the other, the iteration continues with Zipped values in which either Ok1 or Ok2 is false, depending on which sequence ended first.

Zip is a useful building block for adapters that process pairs of sequences. For example, Equal can be defined as:

func Equal[V comparable](x, y Seq[V]) bool {
	for z := range Zip(x, y) {
		if z.Ok1 != z.Ok2 || z.V1 != z.V2 {
			return false
		}
	}
	return true
}

Types

type Pair

type Pair[K any, V any] struct {
	First  K
	Second V
}

func Collect2

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

Collect2 collects key-value pairs from seq into a new slice.

type Zipped

type Zipped[V1, V2 any] struct {
	V1  V1
	Ok1 bool // whether V1 is present (if not, it will be zero)
	V2  V2
	Ok2 bool // whether V2 is present (if not, it will be zero)
}

A Zipped is a pair of zipped values, one of which may be missing, drawn from two different sequences.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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