itertools

package module
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: BSD-3-Clause Imports: 2 Imported by: 5

README

itertools

Package itertools contains iterator functions like Zip.

Examples

package main

import (
    "fmt"
    "slices"

    "github.com/keep94/itertools"
)

func main() {
    lettersIter := slices.Values([]string{"a", "b", "c"})
    numbersIter := slices.Values([]int{1, 2, 3})

    // Prints:
    // a 1
    // b 2
    // c 3
    for letter, number := range itertools.Zip(lettersIter, numbersIter) {
        fmt.Println(letter, number)
    }
}

More documentation and examples can be found here.

Documentation

Overview

Package itertools contains iterator functions like Zip.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func At added in v0.7.0

func At[T any](index int, seq iter.Seq[T]) (element T, ok bool)

At returns the 0-based indexth element of seq. At returns false if seq has index or fewer elements or if index is negative.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/keep94/itertools"
)

func main() {
	seq := slices.Values([]int{10, 13, 16})
	fmt.Println(itertools.At(0, seq))
	fmt.Println(itertools.At(1, seq))
	fmt.Println(itertools.At(2, seq))
	fmt.Println(itertools.At(3, seq))
}
Output:

10 true
13 true
16 true
0 false

func Chain added in v0.4.0

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

Chain returns all the elements in the first sequence followed by all the elements in the second sequence etc.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/keep94/itertools"
)

func main() {
	notes := []string{"do", "re", "mi", "fa", "so"}
	ordinals := []int{1, 2, 3}
	notesIter := slices.Values(notes)
	ordinalsIter := itertools.Chain(
		slices.Values(ordinals), itertools.CycleValues(0))
	for n, o := range itertools.Zip(notesIter, ordinalsIter) {
		fmt.Println(n, o)
	}
}
Output:

do 1
re 2
mi 3
fa 0
so 0

func Count added in v0.2.0

func Count(start, step int) iter.Seq[int]

Count returns start, start + step, start + 2*step, ...

func Cycle

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

Cycle returns the values in seq repeating in an infinite cycle.

func CycleValues added in v0.5.0

func CycleValues[T any](values ...T) iter.Seq[T]

CycleValues returns values repeating in an infinite cycle.

func Drop added in v0.7.0

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

Drop returns everything but the first n elements of seq.

func DropWhile added in v0.7.0

func DropWhile[T any](f Filterer[T], seq iter.Seq[T]) iter.Seq[T]

DropWhile returns everything but the first elements of seq for which f returns true.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/keep94/itertools"
)

func main() {
	seq := slices.Values([]int{1, 2, 3, 4, 5, 1, 2, 3, 4, 5})
	f := func(x int) bool { return x < 4 }
	for x := range itertools.DropWhile(f, seq) {
		fmt.Println(x)
	}
}
Output:

4
5
1
2
3
4
5

func Enumerate added in v0.2.0

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

Enumerate works like enumerate in python. It is equivalent to Zip(Count(0, 1), seq)

Example
package main

import (
	"fmt"
	"slices"

	"github.com/keep94/itertools"
)

func main() {
	notesIter := slices.Values([]string{"do", "re", "mi", "fa", "so"})
	for i, n := range itertools.Enumerate(notesIter) {
		fmt.Println(i, n)
	}
}
Output:

0 do
1 re
2 mi
3 fa
4 so

func Filter

func Filter[T any](f Filterer[T], seq iter.Seq[T]) iter.Seq[T]

Filter returns an iter.Seq[T] that contains all the T values in seq for which f returns true.

func First added in v0.7.0

func First[T any](seq iter.Seq[T]) (first T, ok bool)

First returns the first element of seq or false if seq is empty.

func Map

func Map[T, U any](m Mapper[T, U], seq iter.Seq[T]) iter.Seq[U]

Map returns an iter.Seq[U] which is m applied to each element in seq.

func PairUp

func PairUp[F, S any](seq iter.Seq2[F, S]) iter.Seq[Pair[F, S]]

PairUp converts an iter.Seq2 into an iter.Seq of pairs.

func Take added in v0.3.0

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

Take returns the first n elements of seq.

func TakeWhile added in v0.3.0

func TakeWhile[T any](f Filterer[T], seq iter.Seq[T]) iter.Seq[T]

TakeWhile returns the first elements of seq for which f returns true.

Example
package main

import (
	"fmt"

	"github.com/keep94/itertools"
)

func main() {
	seq := itertools.CycleValues(1, 2, 3, 4, 5)
	f := func(x int) bool { return x < 4 }
	for x := range itertools.TakeWhile(f, seq) {
		fmt.Println(x)
	}
}
Output:

1
2
3

func Zip

func Zip[F, S any](first iter.Seq[F], second iter.Seq[S]) iter.Seq2[F, S]

Zip works like zip in python.

Example
package main

import (
	"fmt"
	"slices"

	"github.com/keep94/itertools"
)

func main() {
	notesIter := slices.Values([]string{"do", "re", "mi", "fa", "so"})
	ordinalsIter := slices.Values([]int{1, 2, 3})
	for n, o := range itertools.Zip(notesIter, ordinalsIter) {
		fmt.Println(n, o)
	}
}
Output:

do 1
re 2
mi 3

Types

type Filterer

type Filterer[T any] func(T) bool

Filterer filters T values.

type Mapper

type Mapper[T, U any] func(T) U

Mapper maps T values to U values.

type Pair

type Pair[F, S any] struct {
	First  F
	Second S
}

Pair represents a pair of values.

Jump to

Keyboard shortcuts

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