itertools

package
v0.1.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package itertools contains methods that are helpful for working with iterators. Requires Golang version 1.23 or later. For more information on iterators see the iter package.

The maps and slices packages already contain methods for converting slices and maps from/to iterators, so this package does not contain any methods to do this (although the Reduce method can be used to convert iterators into maps or slices if desired).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

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

Filter wraps an iter.Seq and drops items that do not match a supplied filtering func.

For every item in seq, filter will be called and passed that item as its sole argument. If that call to filter returns true then that item will be included in the output iter.Seq, otherwise it will be discarded.

The order of items in the output iter.Seq will match the order that they appear in seq.

input := slices.Values([]int{0, 1, 2, 3})
filteredIter := itertools.Filter(input, func(item int) bool {
	return item%2 == 0
})

slices.Collect(filteredIter) // []int{0, 2}

func Filter2

func Filter2[S, T any](seq iter.Seq2[S, T], filter func(S, T) bool) iter.Seq2[S, T]

Filter2 wraps an iter.Seq2 and drops items that do not match a supplied filtering func.

This func works the same as Filter, except that the input and output iterators are iter.Seq2.

input := slices.All([]int{0, 1, 2, 3})
filteredIter := itertools.Filter2(input, func(index, item int) bool {
	return index == 0 || item == 3
})

maps.Collect(filteredIter) // map[int]int{0: 0, 3: 3}

func Map

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

Map creates a new iter.Seq by applying the given mapper func to the given iter.Seq.

For every item in seq, mapper will be called and passed that item as its sole argument. The output of mapper will then be included in the output iter.Seq.

The order of items in the output iter.Seq will match the order of the items that they are mapped from in seq.

input := slices.Values([]int{1, 2, 3})
output := itertools.Map(input, func(item int) string {
  return fmt.Sprintf("%d", item)
})

slices.Collect(output) // []string{"1", "2", "3"}

func Map2

func Map2[In1, In2, Out1, Out2 any](seq iter.Seq2[In1, In2], mapper func(In1, In2) (Out1, Out2)) iter.Seq2[Out1, Out2]

Map2 creates a new iter.Seq2 by applying the given mapper func to the given iter.Seq2.

This func works the same as Map, except that its input and output iterators are iter.Seq2.

input := slices.All([]int{1, 2, 3})
output := itertools.Map2(input, func(index, item int) (int, string) {
	return index, fmt.Sprintf("%d", item)
})

maps.Collect(output) // map[int]string{0: "1", 1: "2", 2: "3"}

func Range

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

Range creates an iter.Seq that will output all integers between start and end, including start and end.

slices.Collect(itertools.Range(1, 5)) // []int{1, 2, 3, 4, 5}

func RangeStep

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

RangeStep creates an iter.Seq that will output all integers between start and end, in increments of step.

slices.Collect(itertools.Range(1, 5, 2)) // []int{1, 3, 5}

start will always be included as the first value, but end will only be included if the distance between start and end is exactly divisable by step.

slices.Collect(itertools.Range(5, 2, -2)) // []int{5, 3}

If step is not a value that moves start closer to end, then the returned iterator will be empty. This is to prevent creation of an iterator that is incrementing in the wrong direction, or that is infinite.

slices.Collect(itertools.Range(1, 5, -1)) // []int(nil)
slices.Collect(itertools.Range(1, 5, 0)) // []int(nil)

func Reduce

func Reduce[In, Out any](seq iter.Seq[In], collector func(Out, In) Out, initial Out) Out

Reduce applies the given collector func to every element of the given iter.Seq and returns the result.

For the first item in seq, collector will be called with two arguments; the value of initial and the item from seq. For every item in seq after the first, collector will be called with the output from the previous call to collector and the item from seq.

The intent behind collector is that it takes each item from seq and add it to an accumulated total; the first argument to collector is the total so far, and the value returned from collector is the new total after the current item has been added into it. initial is used to give this total a starting value.

input := slices.Values([]int{1, 2, 3})
result := itertools.Reduce(input, func(total []int, item int) []int {
	return append(total, item)
}, make([]int, 0, 3))

// result = []int{1, 2, 3}

func Reduce2

func Reduce2[In1, In2, Out any](seq iter.Seq2[In1, In2], collector func(Out, In1, In2) Out, initial Out) Out

Reduce2 applies the given collector func to every element of the given iter.Seq2 and returns the result.

This func works the same as Reduce, except that the input iterator is iter.Seq2.

input := slices.All([]int{1, 2, 3})
result := itertools.Reduce2(input, func(total []int, index, item int) []int {
	return append(total, index+item)
}, make([]int, 0, 3))

// result = []int{1, 3, 5}

Types

This section is empty.

Jump to

Keyboard shortcuts

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