goitertools

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: ISC Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

goitertools

Installation

go get github.com/radeqq007/goitertools

Examples

Cycle
count := 0
for v := range goitertools.Cycle([]string{"A", "B"}) {
  fmt.Print(v, " ")
  count++
  if count == 5 {
    break
  }
}

// Output: A B A B A
Count
for v := range goitertools.Count(10, 5) {
    fmt.Print(v, " ")
    if v >= 25 {
        break
    }
}
// Output: 10 15 20 25
Repeat
for v := range goitertools.Repeat("Hi", 3) {
    fmt.Print(v, " ")
}
// Output: Hi Hi Hi
Filter
nums := []int{10, 15, 20, 25}
isEven := func(idx int, val int) bool { return val%2 == 0 }

for v := range goitertools.Filter(nums, isEven) {
    fmt.Print(v, " ")
}
// Output: 10 20
FilterFalse
nums := []int{10, 15, 20, 25}
isEven := func(idx int, val int) bool { return val%2 == 0 }

for v := range goitertools.FilterFalse(nums, isEven) {
    fmt.Print(v, " ")
}
// Output: 15 25
Compress
data := []string{"Go", "Python", "Rust"}
pick := []bool{true, false, true}

for v := range goitertools.Compress(data, pick) {
    fmt.Print(v, " ")
}
// Output: Go Rust
DropWhile
nums := []int{1, 3, 4, 1} // Drops 1 and 3 because they are odd
isOdd := func(idx int, val int) bool { return val%2 != 0 }

for v := range goitertools.DropWhile(nums, isOdd) {
    fmt.Print(v, " ")
}
// Output: 4 1
TakeWhile
nums := []int{1, 3, 4, 1} // Stops at 4 because it is not odd
isOdd := func(idx int, val int) bool { return val%2 != 0 }

for v := range goitertools.TakeWhile(nums, isOdd) {
    fmt.Print(v, " ")
}
// Output: 1 3
Chain
s1 := []int{1, 2}
s2 := []int{3, 4}

for v := range goitertools.Chain(s1, s2) {
    fmt.Print(v, " ")
}
// Output: 1 2 3 4

Documentation

Overview

Package goitertools provides a collection iterator utilities inspired by Python's itertools library

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain

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

Chain returns an iterator that treats consecutive slices as a single continuous sequence. It yields all elements from the first slice, then the second, and so on, until all provided slices are exhausted.

func Compress

func Compress[T any](items []T, selectors []bool) iter.Seq[T]

Compress returns an iterator that filters elements returning only those that have a corresponding true value in the selectors. Iteration stops when either the 'items' or 'selectors' slice is exhausted.

func Count

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

Count returns an infinite iterator that yields evenly spaced values starting. from the 'start', incrementing by 'step' on each iteration.

func Cycle

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

Cycle returns an infinite iterator that repeatedly yields elements from the slice. It stops immediately if items is empty.

func DropWhile

func DropWhile[T any](items []T, condition func(int, T) bool) iter.Seq[T]

DropWhile returns an iterator that skips elements as long as the condition function returns true. Once the condition returns false, the and all remaining elements are yielded without further evaluation of the condition.

func Filter

func Filter[T any](items []T, condition func(int, T) bool) iter.Seq[T]

Filter returns an iterator that yields elements from the slice for which the condition function returns true. The condition function receives both the index and the value of the element.

func FilterFalse

func FilterFalse[T any](items []T, condition func(int, T) bool) iter.Seq[T]

FilterFalse returns an iterator that yields elements from the slice for which the condition function returns false. The condition function receives both the index and the value of the element.

func Repeat

func Repeat[T any](value T, times int) iter.Seq[T]

Repeat returns an iterator that yields the specified value a fixed number of times. If 'times' is less than 1, the iterator yields nothing.

func TakeWhile

func TakeWhile[T any](items []T, condition func(int, T) bool) iter.Seq[T]

TakeWhile returns an iterator that yields elements from the slice as long as the condition function returns true. It terminates immediately once the condition returns false.

Types

This section is empty.

Jump to

Keyboard shortcuts

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