itertools

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2024 License: MIT Imports: 2 Imported by: 0

README

go-itertools - Python's itertools for Go iterators

tag Go Version License

astonm/go-itertools is a library styled on the Python itertools library which works using Go 1.23+ iterators.

By convention, all of the functions in this library return either iter.Seq or iter.Seq2. Almost all of the functions operate on iter.Seq as input. The most notable exception is for the combinatoric functions which take a slice, constraining their use to finite sequences.

Install

go get github.com/astonm/go-itertools

Usage

This package can be imported under its default name of itertools like so:

import "github.com/astonm/go-itertools"

Then use one of the helpers below, e.g.

counter := itertools.Count()
// 0, 1, 2, ...

If you are using a large number of library functions or are nesting calls, the lengthy itertools can be a bit much. When brevity is better than clarity, we recommend the short name it, i.e.

import it "github.com/astonm/go-itertools"

Then use like so:

cycle := it.Cycle(it.FromSlice([]int{1,2,3}))
// 1, 2, 3, 1, 2, 3, ...

Documentation

GoDoc: https://pkg.go.dev/github.com/astonm/go-itertools

License

Copyright 2024 Aston Motes.

This project is under MIT license.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Accumulate

func Accumulate[T any](s iter.Seq[T], op func(T, T) T, initial T) iter.Seq[T]

func Batched

func Batched[T any](s iter.Seq[T], n int) iter.Seq[[]T]

func Chain

func Chain[T any](seqs ...iter.Seq[T]) iter.Seq[T]

func Combinations

func Combinations[T any](vals []T, r int) iter.Seq[[]T]

func CombinationsWithReplacement

func CombinationsWithReplacement[T any](vals []T, r int) iter.Seq[[]T]

func Compress

func Compress[T any, C comparable](s iter.Seq[T], selectors []C) iter.Seq[T]

func Count

func Count() iter.Seq[int]

func Cycle

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

func DropWhile

func DropWhile[T any](pred func(T) bool, s iter.Seq[T]) iter.Seq[T]

func Enumerate

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

Enumerate takes an iter.Seq and returns an iter.Seq2 pairing a zero-based index with each original sequence value

Example
package main

import (
	"fmt"

	it "github.com/astonm/go-itertools"
)

func main() {
	for i, v := range it.Enumerate(it.NewSeq("a", "b", "c")) {
		fmt.Println(i, v)
	}
}
Output:

0 a
1 b
2 c

func FilterFalse

func FilterFalse[T any](pred func(T) bool, s iter.Seq[T]) iter.Seq[T]

func FromSlice

func FromSlice[T any](vals []T) iter.Seq[T]

FromSlice returns a sequence of values matching the sequence of slice values given as input

Example
package main

import (
	"fmt"

	it "github.com/astonm/go-itertools"
)

func main() {
	for v := range it.FromSlice([]int{1, 2, 3}) {
		fmt.Println(v)
	}
}
Output:

1
2
3

func GroupBy

func GroupBy[T comparable](s iter.Seq[T]) iter.Seq2[T, iter.Seq[T]]

func Map

func Map[T any, U any](mapper func(T) U, s iter.Seq[T]) iter.Seq[U]

func NewSeq

func NewSeq[T any](vals ...T) iter.Seq[T]

NewSeq returns a sequence of values matching the sequence of values given as input

Example
package main

import (
	"fmt"

	it "github.com/astonm/go-itertools"
)

func main() {
	for v := range it.NewSeq(1, 2, 3) {
		fmt.Println(v)
	}
}
Output:

1
2
3

func Pairwise

func Pairwise[T any](s iter.Seq[T]) iter.Seq2[T, T]

func Permutations

func Permutations[T any](vals []T, r int) iter.Seq[[]T]

func Product

func Product[T any](pool ...[]T) iter.Seq[[]T]

func ProductRepeat

func ProductRepeat[T any](vals []T, repeat int) iter.Seq[[]T]

func PullZip3

func PullZip3[T any, U any, V any](s0 iter.Seq[T], s1 iter.Seq[U], s2 iter.Seq[V]) (func() (T, U, V, bool), func())

func PullZip4

func PullZip4[T any, U any, V any, W any](s0 iter.Seq[T], s1 iter.Seq[U], s2 iter.Seq[V], s3 iter.Seq[W]) (func() (T, U, V, W, bool), func())

func Repeat

func Repeat[T any](val T, n int) iter.Seq[T]

func Slice

func Slice[T any](s iter.Seq[T], start, end int) iter.Seq[T]

func Take

func Take[T any](s iter.Seq[T], n int) iter.Seq[T]

func TakeWhile

func TakeWhile[T any](pred func(T) bool, s iter.Seq[T]) iter.Seq[T]

func Tee

func Tee[T any](s iter.Seq[T]) (iter.Seq[T], iter.Seq[T])

func Zip

func Zip[T any, U any](s0 iter.Seq[T], s1 iter.Seq[U]) iter.Seq2[T, U]

Types

This section is empty.

Jump to

Keyboard shortcuts

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