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: 3 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 Cycle

func Cycle(entry any) (iterator *cycler, err error)

Cycle creates an iterator that cycles through a sequence of values.

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

1
2
3
1
2
3
1
2
3
1

func Flatten

func Flatten(entry any) (output []any, err error)

Flatten takes a list of lists and flattens it into a single list.

Example
entry := []any{1, []any{2, []int{3}}}
f, _ := Flatten(entry)
fmt.Println(f)
Output:

[1 2 3]

func NewCycler

func NewCycler(entry []any) *cycler

NewCycler creates a new cycler iterator from a slice.

func Repeat

func Repeat(entry any) *repeater

Repeat creates a repeater that repeats entry.

Example
r := Repeat("233")
for i := 0; i < 3; i++ {
	fmt.Println(r.Next())
}
fmt.Println(r.Repeat(3))
Output:

233
233
233
[233 233 233]

func Reversed

func Reversed(entry any) (output any, err error)

Reversed returns a reversed copy of the input sequence.

Example
// string
s := "hello world"
r, _ := Reversed(s)
fmt.Println(r)
// array
arr := [...]int{1, 2, 3, 4, 5}
r, _ = Reversed(arr)
fmt.Println(r)
Output:

dlrow olleh
[5 4 3 2 1]

func Slice

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

Slice returns a slice of the given entry, with the given start, end, and step from array, slice, or string.

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 Counter

type Counter struct {
	// contains filtered or unexported fields
}

Counter is an iterator that counts in a specific step, it can be used to generate a sequence of numbers, or to iterate over a range of numbers.

Example
// reverse counting from 10 with step 2
c := NewCounter(10, 2, true)
for i := 0; i < 3; i++ {
	fmt.Println(c.Count())
}
Output:

10
8
6

func NewCounter

func NewCounter(start, step int, reverse bool) *Counter

NewCounter creates a new Counter with the given start, step and a specified order.

func (*Counter) Count

func (iter *Counter) Count() int

Count return the next expected value through counting.

Example
// counting from 1 with step 2
c := NewCounter(1, 2, false)
for i := 0; i < 5; i++ {
	fmt.Println(c.Count())
}
Output:

1
3
5
7
9

func (*Counter) Jump

func (iter *Counter) Jump(n int) int

Jump will do Count n times and return the new value.

Example
// counting from 1 with step 2
c := NewCounter(1, 2, false)
fmt.Println(c.Count())
fmt.Println(c.Jump(3))
fmt.Println(c.Count())
Output:

1
7
9

func (*Counter) Reset

func (iter *Counter) Reset()

Reset the iterator to its initial state.

Example
// counting from 1 with step 2
c := NewCounter(1, 2, false)
for i := 0; i < 3; i++ {
	fmt.Println(c.Count())
}
c.Reset()
for i := 0; i < 3; i++ {
	fmt.Println(c.Count())
}
Output:

1
3
5
1
3
5

type Iterator

type Iterator interface {
	// Next moves the pointer to the next element and returns true if there is a next element or false otherwise.
	Next() bool
	// Value returns the element pointed by the pointer.
	Value() any
	// Pour returns a slice of all remaining elements in the iterator.
	Pour() any
}

Iterator is an interface that defines the behavior of an iterator.

func Accumulate

func Accumulate(handler, entry any) (iterator Iterator, err error)

Accumulate applies a function to the elements of an iterable and returns an iterator that accumulates the results.

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

1
3
6

func Chain

func Chain(entries ...any) (iterator Iterator, err error)

Chain creates an iterator that chains multiple sequences together.

Example
arr := []int{1, 2, 3}
seq := [3]any{1, "a", true}
str := "hello"
iter, _ := Chain(arr, seq, str)
for iter.Next() {
	fmt.Println(iter.Value())
}
Output:

1
2
3
1
a
true
h
e
l
l
o

func DropWhile

func DropWhile(condition func(any) bool, entry any) (iterator Iterator, err error)

DropWhile returns an iterator that drops elements from the iterable as long as the condition is true.

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

0
4
5
6

func Enumerate

func Enumerate(entry any, start, end, step int) (iterator Iterator, err error)

Enumerate creates an iterator that iterates over the given slice, start, end, and step.

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]

func Filter

func Filter(filter, entry any) (iterator Iterator, err error)

Filter creates an iterator that filters a sequence based on a given function.

func GroupBy

func GroupBy(entry any, by func(any) any) (iterator Iterator, err error)

GroupBy groups the elements of the given list by the given key function and returns an iterator of groups.

Example
entry := [][2]int{{1, 2}, {1, 3}, {2, 4}, {2, 5}, {3, 6}, {3, 7}}
f := func(x any) any {
	return x.([2]int)[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(handler, entry any) (iterator Iterator, err error)

Map applies the provided handler function to each element of the input sequence and returns an iterator that produces the results.

func NewAccumulator

func NewAccumulator(entry []any, handler func(any, any) any) Iterator

NewAccumulator creates a new accumulator iterator from a slice of elements and a function to apply to each pair of elements.

func NewListConvertor

func NewListConvertor(entry []any, handler func(any) any) Iterator

NewListConvertor creates a new listConvertor iterator.

func NewListEnumerator

func NewListEnumerator(entry []any, start, end, step int) Iterator

NewListEnumerator creates a new listEnumerator with the given slice, start, end, and step.

func NewListFilter

func NewListFilter(entry []any, filter func(any) bool) Iterator

NewListFilter creates a new listFilter iterator.

func NewListIterator

func NewListIterator(entry []any) Iterator

NewListIterator creates a new Iterator for a slice of any type.

func NewPairIterator

func NewPairIterator(entry []any) Iterator

NewPairIterator creates a new pairIterator from a slice.

func NewSliceIterator

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

NewSliceIterator creates a new slice iterator with the given slice, start, end, and step.

func NewZipIterator

func NewZipIterator(entries [][]any, length int) Iterator

NewZipIterator creates a new zipIterator.

func PairWise

func PairWise(entry any) (iterator Iterator, err error)

PairWise returns an iterator that returns pairs of elements from array, slice or string.

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 TakeWhile

func TakeWhile(condition func(any) bool, entry any) (iterator Iterator, err error)

TakeWhile returns an iterator that takes elements from the iterable as long as the condition is true.

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

1
2
3

func Zip

func Zip(entries ...any) (iterator Iterator, err error)

Zip creates an iterator that iterates over the elements of the given sequences in parallel, and the iteration will end when the shortest input sequence is exhausted.

Example
arr := [3]int{1, 2, 3}
str := "hello"
sli := []any{1, "a", true}
iter, _ := Zip(arr, str, sli)
for iter.Next() {
	fmt.Println(iter.Value())
}
Output:

[1 h 1]
[2 e a]
[3 l true]

func ZipLongest

func ZipLongest(entries ...any) (iterator Iterator, err error)

ZipLongest creates an iterator that iterates over the elements of the given sequences in parallel, and the iteration will end when the longest input sequence is exhausted, filling missing values with nil.

Example
arr := [3]int{1, 2, 3}
str := "hello"
sli := []any{1, "a", true}
iter, _ := ZipLongest(arr, str, sli)
for iter.Next() {
	fmt.Println(iter.Value())
}
Output:

[1 h 1]
[2 e a]
[3 l true]
[<nil> l <nil>]
[<nil> o <nil>]

Jump to

Keyboard shortcuts

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