Documentation
¶
Overview ¶
Package itertools provides iterator functions to create iterators and perform common operations on iterables.
Index ¶
- func Cycle(entry any) (iterator *cycler, err error)
- func Flatten(entry any) (output []any, err error)
- func NewCycler(entry []any) *cycler
- func Repeat(entry any) *repeater
- func Reversed(entry any) (output any, err error)
- func Slice(entry any, start, end, step int) (slice any, err error)
- type Counter
- type Iterator
- func Accumulate(handler, entry any) (iterator Iterator, err error)
- func Chain(entries ...any) (iterator Iterator, err error)
- func DropWhile(condition func(any) bool, entry any) (iterator Iterator, err error)
- func Enumerate(entry any, start, end, step int) (iterator Iterator, err error)
- func Filter(filter, entry any) (iterator Iterator, err error)
- func GroupBy(entry any, by func(any) any) (iterator Iterator, err error)
- func Map(handler, entry any) (iterator Iterator, err error)
- func NewAccumulator(entry []any, handler func(any, any) any) Iterator
- func NewListConvertor(entry []any, handler func(any) any) Iterator
- func NewListEnumerator(entry []any, start, end, step int) Iterator
- func NewListFilter(entry []any, filter func(any) bool) Iterator
- func NewListIterator(entry []any) Iterator
- func NewPairIterator(entry []any) Iterator
- func NewSliceIterator(entry []any, start, end, step int) Iterator
- func NewZipIterator(entries [][]any, length int) Iterator
- func PairWise(entry any) (iterator Iterator, err error)
- func TakeWhile(condition func(any) bool, entry any) (iterator Iterator, err error)
- func Zip(entries ...any) (iterator Iterator, err error)
- func ZipLongest(entries ...any) (iterator Iterator, err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cycle ¶
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 ¶
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]
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 ¶
NewCounter creates a new Counter with the given start, step and a specified order.
func (*Counter) Count ¶
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
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 ¶
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 ¶
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 ¶
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 ¶
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 GroupBy ¶
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 ¶
Map applies the provided handler function to each element of the input sequence and returns an iterator that produces the results.
func NewAccumulator ¶
NewAccumulator creates a new accumulator iterator from a slice of elements and a function to apply to each pair of elements.
func NewListConvertor ¶
NewListConvertor creates a new listConvertor iterator.
func NewListEnumerator ¶
NewListEnumerator creates a new listEnumerator with the given slice, start, end, and step.
func NewListFilter ¶
NewListFilter creates a new listFilter iterator.
func NewListIterator ¶
NewListIterator creates a new Iterator for a slice of any type.
func NewPairIterator ¶
NewPairIterator creates a new pairIterator from a slice.
func NewSliceIterator ¶
NewSliceIterator creates a new slice iterator with the given slice, start, end, and step.
func NewZipIterator ¶
NewZipIterator creates a new zipIterator.
func PairWise ¶
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 ¶
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 ¶
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 ¶
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>]