itertools

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package itertools provides iterator functions to create iterators and perform common operations on iterables.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Slice

func Slice[T any](entry []T, start, end, step int) (slice []T, err error)

Slice returns a new slice by iterating over the input slice based on the start, end, and step parameters.

Example
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
s, _ := Slice(arr, 5, 2, -1)
fmt.Println(s)
Output:

[6 5 4 3]

Types

type Accumulator

type Accumulator[T any] interface {
	// Next advances the iterator and returns true if there are more elements.
	Next() bool
	// Value returns the current accumulated value.
	Value() T
	// Pour returns the final accumulated value by iterating over all elements.
	Pour() T
}

Accumulator is an interface for iterating over a sequence of values and accumulating them using a function.

func Accumulate

func Accumulate[T any](entry []T, handler func(T, T) T) Accumulator[T]

Accumulate function returns a new Accumulator instance.

Example
arr := []int{1, 2, 3}
f := func(x, y int) int {
	return x + y
}
iter := Accumulate(arr, f)
fmt.Println(iter.Value())
for iter.Next() {
	fmt.Println(iter.Value())
}
Output:

1
3
6

type Enumerator

type Enumerator[T any] interface {
	// Next advances the iterator to the next item in the collection and returns true if there is another item to iterate over.
	Next() bool
	// Value returns the current item in the collection.
	Value() *enumItem[T]
	// Pour iterates over the entire collection and returns all items.
	Pour() []*enumItem[T]
}

Enumerator is an interface for iterating over a collection of items.

func Enumerate

func Enumerate[T any](entry []T, start, end, step int) (iterator Enumerator[T], err error)

Enumerate creates and returns an iterator for the given collection with specified start, end, and step parameters.

Example
arr := []int{1, 2, 3, 4, 5}
iter, _ := Enumerate(arr, 0, 4, 2)
for iter.Next() {
	fmt.Println(*iter.Value())
}
Output:

{0 1}
{2 3}
{4 5}

type ListIterator

type ListIterator[T any] interface {
	// Next moves the iterator to the next element in the list and returns true if there is a next element, false otherwise
	Next() bool
	// Value returns the current value of the element the iterator is pointing to
	Value() T
	// Pour iterates over the remaining elements in the list and returns them as a slice
	Pour() []T
}

ListIterator is an interface for iterating over a list of elements

func Chain

func Chain[T any](entries ...[]T) ListIterator[T]

Chain returns a ListIterator for iterating over the given slices of type T.

Example
arr := []int{1, 2, 3}
seq := []int{4, 5, 6}
iter := Chain(arr, seq)
for iter.Next() {
	fmt.Println(iter.Value())
}
Output:

1
2
3
4
5
6

func Cycle

func Cycle[T any](entry []T) ListIterator[T]

Cycle creates and returns a ListIterator using the given slice as the initial entries.

Example
entry := []int{1, 2, 3}
c := Cycle(entry)
for i := 0; i < 10; i++ {
	c.Next()
	fmt.Println(c.Value())
}
Output:

1
2
3
1
2
3
1
2
3
1

func DropWhile

func DropWhile[T any](condition func(T) bool, entry []T) ListIterator[T]

DropWhile takes a condition function and a slice of type T, and returns a ListIterator[T] that iterates over the input slice after dropping elements while the condition is true.

Example
arr := []int{1, 2, 3, 0, 4, 5, 6}
f := func(x int) bool {
	return x > 0
}
iter := DropWhile(f, arr)
for iter.Next() {
	fmt.Println(iter.Value())
}
Output:

0
4
5
6

func GroupBy

func GroupBy[T any, U comparable](entry []T, by func(T) U) ListIterator[[]T]

GroupBy is a generic function that groups the elements of the input slice based on a given criterion function. It takes a slice of type T and a function that extracts a key of type U from each element, and returns a ListIterator of grouped elements.

Example
entry := [][2]int{{1, 2}, {1, 3}, {2, 4}, {2, 5}, {3, 6}, {3, 7}}
f := func(x [2]int) int {
	return x[0]
}
g := GroupBy(entry, f)
for g.Next() {
	fmt.Println(g.Value())
}
Output:

[[1 2] [1 3]]
[[2 4] [2 5]]
[[3 6] [3 7]]

func Map

func Map[E, R any](handler func(E) R, entry []E) ListIterator[R]

Map is a function that takes a handler function and a slice of elements of type E, and returns a ListIterator of type R

Example
entry := []int{1, 2, 3}
f := func(x int) float64 {
	return float64(x) / 2
}
iter := Map(f, entry)
for iter.Next() {
	fmt.Println(iter.Value())
}
Output:

0.5
1
1.5

func NewListIterator

func NewListIterator[T any](entry []T) ListIterator[T]

NewListIterator creates a new ListIterator for the given list of elements

func NewSliceIterator

func NewSliceIterator[T any](entry []T, start, end, step int) ListIterator[T]

NewSliceIterator creates and returns a new slice iterator.

func PairWise

func PairWise[T any](entry []T) ListIterator[[2]T]

PairWise is a function that creates and returns a new pairIterator for the given input slice.

Example
entry := []int{1, 2, 3, 4, 5}
iter := PairWise(entry)
for iter.Next() {
	fmt.Println(iter.Value())
}
Output:

[1 2]
[2 3]
[3 4]
[4 5]

func Repeat

func Repeat[T any](entry T, count int) ListIterator[T]

Repeat creates a new repeater instance to iterate over the given element 'count' number of times.

Example
iter := Repeat("233", 3)
for iter.Next() {
	fmt.Println(iter.Value())
}
Output:

233
233
233

func Reversed

func Reversed[T any](entry []T) ListIterator[T]

Reversed returns a new iterator for iterating over a slice in reverse order.

func TakeWhile

func TakeWhile[T any](condition func(T) bool, entry []T) ListIterator[T]

TakeWhile takes a condition function and a slice of type T, and returns a ListIterator[T] that iterates over the input slice only while the condition is true.

Example
arr := []int{1, 2, 3, 0, 4, 5, 6}
f := func(x int) bool {
	return x > 0
}
iter := TakeWhile(f, arr)
for iter.Next() {
	fmt.Println(iter.Value())
}
Output:

1
2
3

func Zip

func Zip[T any](entries ...[]T) (iterator ListIterator[[]T], err error)

Zip creates an iterator for paired elements from multiple collections of equal length.

Example
arr := []int{1, 2, 3}
arr2 := []int{4, 5, 6}
arr3 := []int{-1, -2}
iter, _ := Zip(arr, arr2, arr3)
for iter.Next() {
	fmt.Println(iter.Value())
}
Output:

[1 4 -1]
[2 5 -2]

func ZipLongest

func ZipLongest[T any](entries ...[]T) (iterator ListIterator[[]T], err error)

ZipLongest creates an iterator for paired elements from multiple collections of unequal length.

Example
arr := []int{1, 2, 3}
arr2 := []int{4, 5, 6}
arr3 := []int{-1, -2}
iter, _ := ZipLongest(arr, arr2, arr3)
for iter.Next() {
	fmt.Println(iter.Value())
}
Output:

[1 4 -1]
[2 5 -2]
[3 6 0]

type ZipIterator

type ZipIterator[T, U any] interface {
	// Next advances the iterator to the next paired elements.
	Next() bool
	// Value returns the current paired elements.
	Value() *zipPair[T, U]
	// Pour returns all remaining paired elements.
	Pour() []*zipPair[T, U]
}

ZipIterator is an interface for iterating over paired elements from two collections.

func ZipPair

func ZipPair[T, U any](entry1 []T, entry2 []U) ZipIterator[T, U]

ZipPair creates an iterator for paired elements from two collections of equal length.

Example
arr := []int{1, 2, 3}
arr2 := []string{"a", "b"}
iter := ZipPair(arr, arr2)
for iter.Next() {
	fmt.Println(*iter.Value())
}
Output:

{1 a}
{2 b}

func ZipPairLongest

func ZipPairLongest[T, U any](entry1 []T, entry2 []U) ZipIterator[T, U]

ZipPairLongest creates an iterator for paired elements from two collections of unequal length.

Example
arr := []int{1, 2, 3}
arr2 := []string{"a", "b"}
iter := ZipPairLongest(arr, arr2)
for iter.Next() {
	fmt.Println(*iter.Value())
}
Output:

{1 a}
{2 b}
{3 }

Jump to

Keyboard shortcuts

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