iterator

package module
v0.0.0-...-1d82695 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2023 License: MIT Imports: 2 Imported by: 11 modules, 16 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Golang Iterator

一、简介

Golang迭代器模式的定义与实现,用于在一些适合迭代器的场景使用。

二、安装

go get -u github.com/golang-infrastructure/go-iterator

三、Example

slice := []int{1, 2, 3, 4, 5}
iterator := NewSliceIterator(slice)
toSlice := ToSlice[int](iterator)
assert.Equal(t, slice, toSlice)

四、API

Iterator[T any]

// Iterator 表示一个迭代器,迭代器的元素类型是T
type Iterator[T any] interface {

	// Next 将迭代器的指针往后移动一个,同时返回指针当前指向的位置是否有元素
	Next() bool

	// Value 返回指针指向的元素,如果没有元素的话返回对应类型的零值
	Value() T
}

IteratorWithIndex[T any]

IteratorWithKey[T any]

ChainIterator[T any]

用来把多个迭代器组合为一个:

	iteratorA := NewRangeIterator(0, 5)
	iteratorB := NewRangeIterator(5, 10)

	iterator := NewChainIterator[int](iteratorA, iteratorB)
	slice := ToSlice[int](iterator)
	t.Log(slice)

ChannelIterator[T any]

用来把channel封装为迭代器:

	channel := make(chan int, 3)
	channel <- 1
	channel <- 2
	channel <- 3
	close(channel)

	iterator := NewChannelIterator(channel)
	slice := ToSlice[int](iterator)
	t.Log(slice)

CycleIterator[T any]

用来无限循环返回某个序列:

	iterator := NewCycleIterator(1, 2, 3)
	for i := 0; i < 100 && iterator.Next(); i++ {
		t.Log(iterator.Value())
	}

MapIterator[K comparable, V any]

用来迭代Map:

	m := make(map[string]int, 0)
	m["a"] = 1
	m["b"] = 2
	m["c"] = 3
	iterator := NewMapIterator(m)
	slice := ToSlice[*MapEntry[string, int]](iterator)
	t.Log(slice)

RangeIterator

用来返回一个范围:

	begin := -100
	end := 100
	iterator := NewRangeIterator(begin, end)
	slice := ToSlice[int](iterator)

	exceptedSlice := make([]int, 0)
	for begin < end {
		exceptedSlice = append(exceptedSlice, begin)
		begin++
	}

	assert.Equal(t, exceptedSlice, slice)

SliceIterator[T any]

用来把切片包装为迭代器:

	slice := []int{1, 2, 3, 4, 5}
	iterator := NewSliceIterator(slice)
	toSlice := ToSlice[int](iterator)
	assert.Equal(t, slice, toSlice)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrIsNotIterator = errors.New("is not iterator")

Functions

func DoIterator

func DoIterator[T any](iterator Iterator[T], iteratorFunc func(value T) bool)

DoIterator 遍历一个迭代器

func IsIterator

func IsIterator[T any](structObject any) bool

IsIterator 判断给定的struct是否实现了迭代器

func ToChannel

func ToChannel[T any](iterator Iterator[T]) chan T

ToChannel 把迭代器转为channel

func ToSlice

func ToSlice[T any](iterator Iterator[T]) []T

ToSlice 把迭代器转为切片

Types

type ChainIterator

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

ChainIterator 把多个迭代器组合为一个迭代器,

func NewChainIterator

func NewChainIterator[T any](iterators ...Iterator[T]) *ChainIterator[T]

func (*ChainIterator[T]) Next

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

func (*ChainIterator[T]) Value

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

type ChannelIterator

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

ChannelIterator 用于把一个channel封装为iterator

func NewChannelIterator

func NewChannelIterator[T any](channel <-chan T) *ChannelIterator[T]

func (*ChannelIterator[T]) Next

func (x *ChannelIterator[T]) Next() bool

func (*ChannelIterator[T]) Value

func (x *ChannelIterator[T]) Value() T

type CycleIterator

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

CycleIterator 把一个给定的序列无限循环

func NewCycleIterator

func NewCycleIterator[T any](slice ...T) *CycleIterator[T]

func (*CycleIterator[T]) Next

func (x *CycleIterator[T]) Next() bool

func (*CycleIterator[T]) Value

func (x *CycleIterator[T]) Value() T

type Iterable

type Iterable[T any] interface {

	// Iterator 返回当前对象的迭代器
	Iterator() Iterator[T]
}

Iterable 用于表示是可迭代的,拥有一个Iterator方法,调用时返回一个迭代器

type Iterator

type Iterator[T any] interface {

	// Next 将迭代器的指针往后移动一个,同时返回指针当前指向的位置是否有元素
	Next() bool

	// Value 返回指针指向的元素,如果没有元素的话返回对应类型的零值
	Value() T
}

Iterator 表示一个迭代器,迭代器的元素类型是T

func CastToIterator

func CastToIterator[T any](structObject any) (Iterator[T], error)

CastToIterator 转换为迭代器

func FromChannel

func FromChannel[T any](channel <-chan T) Iterator[T]

func FromSlice

func FromSlice[T any](slice []T) Iterator[T]

type IteratorWithIndex

type IteratorWithIndex[T any] interface {
	Iterator[T]
	Index() int
	Begin()
	First() bool
	NextTo(func(index int, value T) bool) bool
}

type IteratorWithKey

type IteratorWithKey[T any] interface {
	Iterator[T]
	Key() T
	Begin()
	First() bool
	NextTo(func(key T, value T) bool) bool
}

type MapEntry

type MapEntry[K, V any] struct {
	Key   K
	Value V
}

func (*MapEntry[K, V]) GetKey

func (x *MapEntry[K, V]) GetKey() K

func (*MapEntry[K, V]) GetValue

func (x *MapEntry[K, V]) GetValue() V

type MapIterator

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

MapIterator 用于遍历一个Map的迭代器

func NewMapIterator

func NewMapIterator[K comparable, V any](m map[K]V) *MapIterator[K, V]

func (*MapIterator[K, V]) Next

func (x *MapIterator[K, V]) Next() bool

func (*MapIterator[K, V]) Value

func (x *MapIterator[K, V]) Value() *MapEntry[K, V]

type RangeIterator

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

RangeIterator 范围递增的迭代器

func NewRangeIterator

func NewRangeIterator(begin, end int) *RangeIterator

NewRangeIterator [begin, end)的左闭右开区间

func (*RangeIterator) Next

func (x *RangeIterator) Next() bool

func (*RangeIterator) Value

func (x *RangeIterator) Value() int

type ReverseIteratorWithIndex

type ReverseIteratorWithIndex[T any] interface {
	IteratorWithIndex[T]
	Prev() bool
	End()
	Last() bool
	PrevTo(func(index int, value interface{}) bool) bool
}

type ReverseIteratorWithKey

type ReverseIteratorWithKey[T any] interface {
	IteratorWithKey[T]
	Prev() bool
	End()
	Last() bool
	PrevTo(func(key interface{}, value interface{}) bool) bool
}

type SliceIterator

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

SliceIterator 用于把切片封装为迭代器

func NewSliceIterator

func NewSliceIterator[T any](slice []T) *SliceIterator[T]

func (*SliceIterator[T]) Next

func (x *SliceIterator[T]) Next() bool

func (*SliceIterator[T]) Value

func (x *SliceIterator[T]) Value() T

Jump to

Keyboard shortcuts

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