iterator

package
v0.0.0-...-f2fae0e Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package iterator provides a generic iterator interface and methods to manipulate it.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChanFromIter

func ChanFromIter[T any](it Iterator[T]) <-chan T

func CollectFromIter

func CollectFromIter[T any](it Iterator[T]) []T

CollectFromIter returns all the values of the iterator as a slice.

Types

type ChainIterator

type ChainIterator[T any] struct {
	// contains filtered or unexported fields
}

func Chain

func Chain[T any](it1, it2 Iterator[T], its ...Iterator[T]) *ChainIterator[T]

Chain returns an iterator that chains the elements of the given iterators.

func (*ChainIterator[T]) Chan

func (ci *ChainIterator[T]) Chan() <-chan T

Chan returns a channel that will receive the elements of the underlying iterator.

func (*ChainIterator[T]) Collect

func (ci *ChainIterator[T]) Collect() []T

Collect returns all remaining elements as a slice.

func (*ChainIterator[T]) Next

func (ci *ChainIterator[T]) Next() bool

Next advances the iterator to the next element.

func (*ChainIterator[T]) Value

func (ci *ChainIterator[T]) Value() T

Value returns the current element.

type Channeler

type Channeler[T any] interface {
	Iterator[T]
	// Chan returns a channel that will receive the elements of the underlying iterator.
	Chan() <-chan T
}

type Chunk

type Chunk[T any] struct {
	// contains filtered or unexported fields
}

Chunk represents a group of consecutive elements of a ChunkIterator of a given size.

func (*Chunk[T]) Chan

func (c *Chunk[T]) Chan() <-chan T

Chan returns a channel that will receive the elements of the chunk.

func (*Chunk[T]) Collect

func (c *Chunk[T]) Collect() []T

Collect returns all elements of the chunk as a slice.

func (*Chunk[T]) Detach

func (c *Chunk[T]) Detach() *Chunk[T]

Detach separates the chunk from the underlying iterator. The elements of the chunk will be placed in a newly allocated slice, and the chunk iterator will no longer read from the underlying iterator but instead will start reading from the beginning of the newly allocated slice. Addtionally, detaching the chunk will advance the underlying iterator to the end of the chunk.

func (*Chunk[T]) Next

func (c *Chunk[T]) Next() bool

Next advances the iterator and returns true if there is a next element, false otherwise.

func (*Chunk[T]) Value

func (c *Chunk[T]) Value() T

Value returns the current element. If the chunk iterator is exhausted, the value is undefined.

type ChunkIterator

type ChunkIterator[T any] struct {
	// contains filtered or unexported fields
}

func Chunks

func Chunks[T any](it Iterator[T], size int) *ChunkIterator[T]

Chunk returns an iterator that groups consecutive elements of the input iterator into iterable chunks of the given size.

Example
package main

import (
	"fmt"

	"github.com/vpraid/itertools/iterator"
	"github.com/vpraid/itertools/source"
)

func main() {
	// This example demonstrates how to use Chunks to iterate over a slice in
	// chunks of a given size.
	it := iterator.Chunks[int](source.Slice([]int{1, 2, 3, 4, 5}), 2)
	for it.Next() {
		chunk := it.Value()
		fmt.Println(chunk.Collect())
	}

}
Output:

[1 2]
[3 4]
[5]

func (*ChunkIterator[T]) Next

func (ci *ChunkIterator[T]) Next() bool

Next skips all elements within the current chunk until the next chunk is found, then returns true if there is such a chunk, false if the underlying iterator was exhausted.

func (*ChunkIterator[T]) Value

func (ci *ChunkIterator[T]) Value() *Chunk[T]

Value returns the current chunk.

type Collector

type Collector[T any] interface {
	Iterator[T]
	// Collect returns the elements of the underlying iterator as a slice.
	Collect() []T
}

Collector is the interface that provides a way to collect the elements of an iterator into a slice.

type FilterIterator

type FilterIterator[T any] struct {
	// contains filtered or unexported fields
}

FilterIterator is an iterator that filters the elements of af the input iterator.

func Filter

func Filter[T any](it Iterator[T], p func(T) bool) *FilterIterator[T]

Filter returns an iterator that filters the elements of the input iterator with the given predicate.

Example
s := source.Slice([]int{1, 2, 3, 4, 5})
// We need to specify the type of the iterator explicitly because the compiler cannot infer it yet. This is a known
// limitation of the Go compiler which will be fixed in Go 1.21.
fi := Filter[int](s, func(i int) bool { return i%2 == 0 })
for fi.Next() {
	fmt.Println(fi.Value())
}
Output:

2
4

func (*FilterIterator[T]) Bind

func (fi *FilterIterator[T]) Bind(it Iterator[T])

Bind replaces the underlying iterator with the given one.

func (*FilterIterator[T]) Chan

func (fi *FilterIterator[T]) Chan() <-chan T

Chan returns a channel that will receive the elements of the underlying iterator.

func (*FilterIterator[T]) Collect

func (fi *FilterIterator[T]) Collect() []T

Collect returns the elements of the underlying iterator as a slice.

func (*FilterIterator[T]) Next

func (fi *FilterIterator[T]) Next() bool

Next advances the iterator to the next element.

func (*FilterIterator[T]) Value

func (fi *FilterIterator[T]) Value() T

Value returns the current element.

type Group

type Group[T any, K comparable] struct {
	Key K
	// contains filtered or unexported fields
}

Group is an iterable group of consecutive elements having the same key.

Example
package main

import (
	"fmt"

	"github.com/vpraid/itertools/functional"
	"github.com/vpraid/itertools/iterator"
	"github.com/vpraid/itertools/partial"
	"github.com/vpraid/itertools/source"
)

func main() {
	// Transform initial iterator into groups of consecutive elements based on the provided mapping function, then
	// detach the groups and collect them into slices.
	it := functional.Compose3[int, *iterator.Group[int, bool], []int](
		source.Slice[int]([]int{1, 3, 5, 2, 4, 7}),
		partial.GroupBy[int, bool](func(i int) bool { return i%2 == 0 }),
		partial.Map(func(group *iterator.Group[int, bool]) []int { return group.Detach().Collect() }),
	)

	// Iterate over the groups.
	for it.Next() {
		fmt.Println(it.Value())
	}

}
Output:

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

func (*Group[T, K]) Chan

func (g *Group[T, K]) Chan() <-chan T

Chan returns a channel that will receive the elements of the group.

func (*Group[T, K]) Collect

func (g *Group[T, K]) Collect() []T

Collect returns the elements of the group as a slice.

func (*Group[T, K]) Detach

func (g *Group[T, K]) Detach() *Group[T, K]

Detach separates the group from the underlying iterator. The elements of the group will be placed in a newly allocated slice, and the group iterator will no longer read from the underlying iterator but instead will start reading from the beginning of the slice. Addtionally, detaching the group will advance the underlying iterator to the end of the group.

func (*Group[T, K]) Next

func (g *Group[T, K]) Next() bool

Next advances the iterator within the group to the next element. When the group is exhausted, it fuses the group iterator. This means that the group iterator will no longer be able to read from the underlying iterator.

func (*Group[T, K]) Value

func (g *Group[T, K]) Value() T

Value returns the current element.

type GroupByIterator

type GroupByIterator[T any, K comparable] struct {
	// contains filtered or unexported fields
}

GroupByIterator is an iterator that groups consecutive elements of the input iterator into iterable groups.

func GroupBy

func GroupBy[T any, K comparable](it Iterator[T], fn func(t T) K) *GroupByIterator[T, K]

GroupBy returns an GroupByIterator that groups consecutive elements of the input iterator into iterable groups based on the provided mapping function.

Example
package main

import (
	"fmt"

	"github.com/vpraid/itertools/iterator"
	"github.com/vpraid/itertools/source"
)

func main() {
	// GroupBy groups consecutive elements of the input iterator into iterable groups based on the provided mapping
	// function.
	it := iterator.GroupBy[int, bool](
		source.Slice([]int{1, 3, 5, 2, 4, 7}),
		func(i int) bool { return i%2 == 0 },
	)

	// Iterate over the groups.
	for it.Next() {
		group := it.Value()
		fmt.Println(group.Collect())
	}

}
Output:

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

func (*GroupByIterator[T, K]) Bind

func (g *GroupByIterator[T, K]) Bind(it Iterator[T])

Bind replaces the underlying iterator with the given one.

func (*GroupByIterator[T, K]) Next

func (gi *GroupByIterator[T, K]) Next() bool

Next skips all elements within the current group until the next group is found, then returns true if there is such a group, false if the underlying iterator was exhausted.

func (*GroupByIterator[T, K]) Value

func (gi *GroupByIterator[T, K]) Value() *Group[T, K]

Value returns the current group.

type InterleaveIterator

type InterleaveIterator[T any] struct {
	// contains filtered or unexported fields
}

func Interleave

func Interleave[T any](its ...Iterator[T]) *InterleaveIterator[T]

Interleave returns an iterator that interleaves the elements of the input iterators.

func (*InterleaveIterator[T]) Chan

func (ii *InterleaveIterator[T]) Chan() <-chan T

Chan returns a channel that will receive the remaining elements.

func (*InterleaveIterator[T]) Collect

func (ii *InterleaveIterator[T]) Collect() []T

Collect returns all remaining elements as a slice.

func (*InterleaveIterator[T]) Next

func (ii *InterleaveIterator[T]) Next() bool

Next advances the iterator to the next element.

func (*InterleaveIterator[T]) Value

func (ii *InterleaveIterator[T]) Value() T

Value returns the current element.

type Iterator

type Iterator[T any] interface {
	// Next advances the iterator to the next element. It returns false if the underlying iterator was exhausted.
	Next() bool
	// Value returns the current element of the underlying iterator. If the underlying iterator was exhausted, it returns the zero value.
	Value() T
}

Iterator is the interface that provides a way to iterate over a collection of elements of type T.

type MapIterator

type MapIterator[T any, U any] struct {
	// contains filtered or unexported fields
}

MapIterator is an iterator that applies a function to each element of the underlying iterator.

func Map

func Map[T any, U any](it Iterator[T], fn func(t T) U) *MapIterator[T, U]

Map returns a MapIterator that applies fn to each element of the underlying iterator.

Example
s := source.Slice([]string{"a", "bb", "ccc"})
// We need to specify the type of the iterator explicitly because the compiler cannot infer it yet. This is a known
// limitation of the Go compiler which will be fixed in Go 1.21.
m := Map[string](s, func(t string) int { return len(t) })
for m.Next() {
	fmt.Println(m.Value())
}
Output:

1
2
3

func (*MapIterator[T, U]) Bind

func (mi *MapIterator[T, U]) Bind(it Iterator[T])

Bind replaces the underlying iterator with the given one.

func (*MapIterator[T, U]) Chan

func (mi *MapIterator[T, U]) Chan() <-chan U

Chan returns a channel that will receive all elements of the iterator.

func (*MapIterator[T, U]) Collect

func (mi *MapIterator[T, U]) Collect() []U

Collect returns a slice containing all elements of the iterator.

func (*MapIterator[T, U]) Next

func (mi *MapIterator[T, U]) Next() bool

Next advances the iterator to the next element. It returns false if the underlying iterator was exhausted.

func (*MapIterator[T, U]) Value

func (mi *MapIterator[T, U]) Value() U

Value returns the current element of the underlying iterator after applying the mapping function.

type MergeIterator

type MergeIterator[U any] struct {
	// contains filtered or unexported fields
}

func Merge

func Merge[T, S, U any](fn func(T, S) U, it1 Iterator[T], it2 Iterator[S]) *MergeIterator[U]

Merge returns an iterator that merges the elements of the given iterators using the given function.

func (*MergeIterator[U]) Chan

func (mi *MergeIterator[U]) Chan() <-chan U

Chan returns a channel that will receive all elements in the iterator.

func (*MergeIterator[U]) Collect

func (mi *MergeIterator[U]) Collect() []U

Collect return a slice containing all elements in the iterator.

func (*MergeIterator[U]) Next

func (mi *MergeIterator[U]) Next() bool

Next returns true if the iterator has more elements.

func (*MergeIterator[U]) Value

func (mi *MergeIterator[U]) Value() U

Value returns the current element in the iterator.

type Pair

type Pair[T, U any] struct {
	First  T
	Second U
}

Pair represents a pair of elements from two iterators.

type PeekAheadIterator

type PeekAheadIterator[T any] struct {
	// contains filtered or unexported fields
}

PeekAheadIterator is an iterator that allows reading the next element without advancing the iterator.

func PeekAhead

func PeekAhead[T any](it Iterator[T]) *PeekAheadIterator[T]

PeekAhead returns a PeekAheadIterator.

Example
s := source.Slice([]int{1, 2, 3})
// We need to specify the type of the iterator explicitly because the compiler cannot infer it yet. This is a known
// limitation of Go.
it := PeekAhead[int](s)
for it.Next() {
	fmt.Println(it.Value())
	fmt.Println(it.Peek())
}
Output:

1
2
2
3
3
0

func (*PeekAheadIterator[T]) Bind

func (pai *PeekAheadIterator[T]) Bind(it Iterator[T])

Bind replaces the underlying iterator with the given one. Elements of the underlying iterator will not be skipped anew if Next was already called.

func (*PeekAheadIterator[T]) Chan

func (pai *PeekAheadIterator[T]) Chan() <-chan T

Chan returns a channel that will receive the elements of the underlying iterator.

func (*PeekAheadIterator[T]) Collect

func (pai *PeekAheadIterator[T]) Collect() []T

Collect returns the elements of the underlying iterator as a slice.

func (*PeekAheadIterator[T]) Exhausted

func (pai *PeekAheadIterator[T]) Exhausted() bool

Exhausted returns true if the underlying iterator was exhausted.

func (*PeekAheadIterator[T]) Next

func (pai *PeekAheadIterator[T]) Next() bool

Next advances the iterator to the next element. It returns false if the underlying iterator was exhausted. Under the hood it calls the underlying iterator's Next method to advance it one step further than what is visible to the user, while caching the value of the current and next element.

func (*PeekAheadIterator[T]) Peek

func (pai *PeekAheadIterator[T]) Peek() T

Peek returns the cached value of the next element.

func (*PeekAheadIterator[T]) Value

func (pai *PeekAheadIterator[T]) Value() T

Value returns the cached value of the current element.

type SkipIterator

type SkipIterator[T any] struct {
	// contains filtered or unexported fields
}

SkipIterator is an iterator that skips the first n elements of an iterator.

func Skip

func Skip[T any](it Iterator[T], n int) *SkipIterator[T]

Skip returns a SkipIterator for the given n.

Example
s := source.Slice([]int{1, 2, 3, 4, 5})
// We need to specify the type of the iterator explicitly because the compiler cannot infer it yet. This is a known
// limitation of Go.
it := Skip[int](s, 2)
for it.Next() {
	fmt.Println(it.Value())
}
Output:

3
4
5

func (*SkipIterator[T]) Bind

func (si *SkipIterator[T]) Bind(it Iterator[T])

Bind replaces the underlying iterator with the given one.

func (*SkipIterator[T]) Chan

func (it *SkipIterator[T]) Chan() <-chan T

Chan returns a channel that will receive the elements of the underlying iterator.

func (*SkipIterator[T]) Collect

func (it *SkipIterator[T]) Collect() []T

Collect returns the elements of the underlying iterator as a slice.

func (*SkipIterator[T]) Next

func (it *SkipIterator[T]) Next() bool

Next advances the iterator to the next element. It returns false if the underlying iterator was exhausted. It will continue skipping elements until the underlying iterator is exhausted or n elements have been skipped.

func (*SkipIterator[T]) Value

func (it *SkipIterator[T]) Value() T

Value returns the current element of the underlying iterator.

type SkipWhileIterator

type SkipWhileIterator[T any] struct {
	// contains filtered or unexported fields
}

func SkipWhile

func SkipWhile[T any](it Iterator[T], pred func(T) bool) *SkipWhileIterator[T]

SkipWhile returns a SkipWhileIterator for the given predicate.

Example
s := source.Slice([]int{1, 3, 5, 4, 6})
// We need to specify the type of the iterator explicitly because the compiler cannot infer it yet. This is a known
// limitation of the Go compiler which will be fixed in Go 1.21.
it := SkipWhile[int](s, func(i int) bool { return i%2 == 1 })
for it.Next() {
	fmt.Println(it.Value())
}
Output:

4
6

func (*SkipWhileIterator[T]) Bind

func (si *SkipWhileIterator[T]) Bind(it Iterator[T])

Bind replaces the underlying iterator with the given one.

func (*SkipWhileIterator[T]) Chan

func (si *SkipWhileIterator[T]) Chan() <-chan T

Chan returns a channel that will receive the elements of the underlying iterator.

func (*SkipWhileIterator[T]) Collect

func (si *SkipWhileIterator[T]) Collect() []T

Collect returns the elements of the underlying iterator as a slice.

func (*SkipWhileIterator[T]) Next

func (si *SkipWhileIterator[T]) Next() bool

Next advances the iterator to the next element. It returns false if the underlying iterator was exhausted.

func (*SkipWhileIterator[T]) Value

func (si *SkipWhileIterator[T]) Value() T

Value returns the current element of the underlying iterator.

type StepByIterator

type StepByIterator[T any] struct {
	// contains filtered or unexported fields
}

func StepBy

func StepBy[T any](it Iterator[T], step int) *StepByIterator[T]

StepBy returns an iterator that steps through the elements of the input iterator by the given step.

func (*StepByIterator[T]) Chan

func (sbi *StepByIterator[T]) Chan() <-chan T

Chan returns a channel that will receive the remaining elements.

func (*StepByIterator[T]) Collect

func (sbi *StepByIterator[T]) Collect() []T

Collect returns all remaining elements in the iterator.

func (*StepByIterator[T]) Next

func (sbi *StepByIterator[T]) Next() bool

Next advances the iterator to the next element.

func (*StepByIterator[T]) Value

func (sbi *StepByIterator[T]) Value() T

Value returns the current element.

type TakeIterator

type TakeIterator[T any] struct {
	// contains filtered or unexported fields
}

TakeIterator is an iterator that takes the first n elements of an iterator.

func Take

func Take[T any](it Iterator[T], size int) *TakeIterator[T]

Take returns a TakeIterator for the given n.

Example
s := source.Slice([]int{1, 2, 3, 4, 5})
// We need to specify the type of the iterator explicitly because the compiler cannot infer it yet. This is a known
// limitation of the Go compiler which will be fixed in Go 1.21.
ti := Take[int](s, 2)
for ti.Next() {
	fmt.Println(ti.Value())
}
Output:

1
2

func (*TakeIterator[T]) Bind

func (ti *TakeIterator[T]) Bind(it Iterator[T])

Bind replaces the underlying iterator with the given one. If the iterator was partially or fully exhausted, the new iterator will continue where the old one left off. The counter will not change, but the elements returned by Value will be from the new iterator.

func (*TakeIterator[T]) Chan

func (ti *TakeIterator[T]) Chan() <-chan T

Chan returns a channel that will receive the elements of the underlying iterator.

func (*TakeIterator[T]) Collect

func (ti *TakeIterator[T]) Collect() []T

Collect returns the elements of the underlying iterator as a slice.

func (*TakeIterator[T]) Next

func (ti *TakeIterator[T]) Next() bool

Next advances the iterator to the next element. It returns false if the underlying iterator was exhausted.

func (*TakeIterator[T]) Value

func (ti *TakeIterator[T]) Value() T

Value returns the current element of the underlying iterator.

type TakeWhileIterator

type TakeWhileIterator[T any] struct {
	// contains filtered or unexported fields
}

func TakeWhile

func TakeWhile[T any](it Iterator[T], pred func(T) bool) *TakeWhileIterator[T]

TakeWhile returns a TakeWhileIterator for the given predicate.

Example
s := source.Slice([]int{1, 3, 5, 4, 6})
// We need to specify the type of the iterator explicitly because the compiler cannot infer it yet. This is a known
// limitation of the Go compiler which will be fixed in Go 1.21.
it := TakeWhile[int](s, func(i int) bool { return i%2 == 1 })
for it.Next() {
	fmt.Println(it.Value())
}
Output:

1
3
5

func (*TakeWhileIterator[T]) Bind

func (ti *TakeWhileIterator[T]) Bind(it Iterator[T])

Bind replaces the underlying iterator with the given one.

func (*TakeWhileIterator[T]) Chan

func (ti *TakeWhileIterator[T]) Chan() <-chan T

Chan returns a channel that will receive the elements of the underlying iterator.

func (*TakeWhileIterator[T]) Collect

func (ti *TakeWhileIterator[T]) Collect() []T

Collect returns the elements of the underlying iterator as a slice.

func (*TakeWhileIterator[T]) Next

func (ti *TakeWhileIterator[T]) Next() bool

Next advances the iterator to the next element. It returns false if the underlying iterator was exhausted.

func (*TakeWhileIterator[T]) Value

func (ti *TakeWhileIterator[T]) Value() T

Value returns the current element of the underlying iterator.

type Window

type Window[T any] struct {
	// contains filtered or unexported fields
}

func (*Window[T]) Chan

func (w *Window[T]) Chan() <-chan T

Chan returns a channel that will receive the remaining elements.

func (*Window[T]) Collect

func (w *Window[T]) Collect() []T

Collect returns all elements as a slice.

func (*Window[T]) Next

func (w *Window[T]) Next() bool

Next advances the iterator and returns true if there unless there are no more elements to read from the underlying slice.

func (*Window[T]) Value

func (w *Window[T]) Value() T

Value returns the current element's value.

type WindowIterator

type WindowIterator[T any] struct {
	// contains filtered or unexported fields
}

func Windows

func Windows[T any](it Iterator[T], n int) *WindowIterator[T]

func (*WindowIterator[T]) Collect

func (wi *WindowIterator[T]) Collect() []*Window[T]

Collect returns all elements as a slice.

func (*WindowIterator[T]) Next

func (wi *WindowIterator[T]) Next() bool

Next advances the iterator and returns true if there unless there are no more elements to read from the underlying iterator.

func (*WindowIterator[T]) Value

func (wi *WindowIterator[T]) Value() *Window[T]

Value returns the current element's value.

type ZipIterator

type ZipIterator[T, U any] struct {
	// contains filtered or unexported fields
}

ZipIterator is an iterator that iterates over two iterators in parallel.

func Zip

func Zip[T, U any](it1 Iterator[T], it2 Iterator[U]) *ZipIterator[T, U]

Zip returns a ZipIterator.

Example
s1 := source.Slice([]int{1, 2, 3})
s2 := source.Slice([]int{4, 5, 6})
it := Zip[int, int](s1, s2)
for it.Next() {
	pair := it.Value()
	fmt.Println(pair.First, pair.Second)
}
Output:

1 4
2 5
3 6

func (*ZipIterator[T, U]) Chan

func (zi *ZipIterator[T, U]) Chan() <-chan Pair[T, U]

Chan returns a channel that yields the elements of the underlying iterator.

func (*ZipIterator[T, U]) Collect

func (zi *ZipIterator[T, U]) Collect() []Pair[T, U]

Collect returns the elements of the underlying iterator as a slice.

func (*ZipIterator[T, U]) Next

func (zi *ZipIterator[T, U]) Next() bool

Next advances the iterator to the next element. It returns false if one of the underlying iterators was exhausted.

func (*ZipIterator[T, U]) Value

func (zi *ZipIterator[T, U]) Value() Pair[T, U]

Value returns the current element.

Jump to

Keyboard shortcuts

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