iterator

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Collect

func Collect[E any](it Iterator[E]) []E

`Collect`和`Group`理论上可以使用`Map`+`Reduce`实现 提供这两个函数主要是为了更方便使用

func Group

func Group[K comparable, E, R any](it Iterator[E], extract func(E) (K, R)) map[K][]R

`Group` and `Map` not support error handler if source data have some invalid value, should use `Filter` to filter data before call `Group“ or `Map` or `Reduce` if some external resources faile, `extract` or `handler` should panic or return some default value

func Iter

func Iter[E any](it Iterator[E]) iter.Seq[E]

1.23之后,标准库中增加迭代器`iter` 通过`Iter()`创建符合标准库格式的迭代器`iter.Seq[E]` 可以直接使用rang进行遍历

func Iter2

func Iter2[K, V any](it Iterator[lapluma.Pair[K, V]]) iter.Seq2[K, V]

func Reduce

func Reduce[E, R any](it Iterator[E], handler func(R, E) R, initial R) R

Types

type BaseIterator

type BaseIterator[E any] struct {
	// contains filtered or unexported fields
}

func (*BaseIterator[E]) Next

func (it *BaseIterator[E]) Next() (E, bool)

type FilterIterator

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

func (*FilterIterator[E]) Next

func (it *FilterIterator[E]) Next() (E, bool)

type Iterator

type Iterator[E any] interface {
	// !!! 在任何转换迭代器的 Next() 方法内部,严禁使用 for-range 模式从上游迭代器获取数据。
	// 必须使用传统的、最高效的方式
	// 禁用 for-range 约束仅限于`Next()`,若使用 for-rang
	// 每次调用Next都将创建一个iter.Seq对象,Collect等函数需要遍历,则影响不大
	Next() (E, bool)
}

func Filter

func Filter[E any](it Iterator[E], filter func(E) bool) Iterator[E]

func FromMap

func FromMap[K comparable, V any](data map[K]V) Iterator[lapluma.Pair[K, V]]

func FromSlice

func FromSlice[E any](data []E) Iterator[E]

support function to help create iterator from slice and map Iterator 用于串行处理场景,不支持并发,如果需要在并发场景下使用 调用pipe.`FromIterator`将其转换为Pipe

func Map

func Map[E, R any](it Iterator[E], handler func(E) R) Iterator[R]

这里是否可以不要errHandler,由调用者负责保证`hanlder`的正确性 但是如果确实出现了特殊情况handler无法处理,由必须提供手段给调用者

func TryMap

func TryMap[E, R any](it Iterator[E], handler func(E) (R, error)) Iterator[R]

type MapIterator

type MapIterator[E, R any] struct {
	// contains filtered or unexported fields
}

func (*MapIterator[E, R]) Next

func (it *MapIterator[E, R]) Next() (R, bool)

type TryMapIterator

type TryMapIterator[E, R any] struct {
	// contains filtered or unexported fields
}

TryMapIterator 用于处理那些可能会发生错误的操作

func (*TryMapIterator[E, R]) Next

func (it *TryMapIterator[E, R]) Next() (R, bool)

Jump to

Keyboard shortcuts

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