iterator

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2022 License: MIT Imports: 4 Imported by: 0

README

简单的go iterator接口

特点

链式调用
res := iterator.FromArray([]int{1, 2, 3, 4, 5}).Map(func(item any) any {
  return item.(int) + 1
}).Filter(func(item any) bool {
  return item.(int) % 2 == 0
}).Reduce(0, func(result any, item any) any {
  return result.(int) + item.(int)
})

fmt.Println("%d", res)
惰性调用

是一个惰性的iterator接口,需要调用consumer函数才会执行,比如ForEach,Collect, Reduce函数

iter := iterator.FromArray([]int{1, 2, 3, 4, 5}).Map(func(item any) any {
  fmt.Println("%d", item.(int))
  return item.(int) + 1
}).Filter(func(item any) bool {
  fmt.Println("%d", item.(int))
  return item.(int) % 2 == 0
}) // 并不会打印

consumer.Collect[int](iter) // 执行map和filter函数中的打印

方法说明

Iterator

Iterator接口

type Iterator interface {
  Next() (any, bool)
}

Next的第二个返回值为true表示迭代完成,第一个返回值是无效的。

IterWrapper struct

由各种chain方法组成的Iterator,用于装饰Iterator

IterWrapper实现的chain方法

可以在chain中找到对应的实现

名称 签名 说明
Concat func(Iterator) IterWrapper 拼接一个Iterator
Enumerate func() IterWrapper 使用Enumerate包装迭代器返回值
Filter func(func(any) bool) IterWrapper 忽略返回false的项
Inspect func(func(any)) IterWrapper 迭代中执行函数内容,有别于ForEach
Intersperse func(any) IterWrapper 原来的两次迭代中插入一个值
IntersperseWith func(func()any) IterWrapper 原来的两次迭代中插入一个值
Map func(func(any)any) IterWrapper 使用fn中的返回值作为迭代返回值
Scan func(any, func(any, any) any) IterWrapper fn中的返回值是迭代返回值,并且是fn的第一个参数,有别于Reduce
Skip func(uint) IterWrapper 跳过迭代的前n项
Take func(uint) IterWrapper 只迭代前n项
IterWrapper实现的consumer方法

可以在consumer中找到对应的实现

名称 签名 说明
All func(func(any)bool) bool 如果fn都返回true,则All返回true,否则返回false
Any func(func(any)bool) bool 如果fn都返回false,则Any返回false,否则返回true
Find func(func(any) bool) (any, bool) 返回fn返回true的第一个对象,没找到,第二个返回值为false
ForEach func(func(any)) 消费迭代器,执行fn
Position func(func(any) bool) int 返回fn返回true的第一个对象的下标,没找到,第二个返回值为false
Reduce func(any, func(any, any) any) any 聚合函数
FromArray func(any) IterWrapper

将一个arrayslice转换成IterWrapper

FromMap func(any) IterWrapper

将一个map转换成IterWrapper

consumer函数
Collect funcT any []T

将迭代器返回指定类型的数组,如果无法转换,则返回对应类型的零值

iter := iterator.FromArray([]int{1, 2, 3, 4, 5})

consumer.Collect[int](iter) // 返回 []int{1, 2, 3, 4, 5}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IterWrapper

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

func EmptyWrapper

func EmptyWrapper() *IterWrapper

func FromArray

func FromArray(array any) *IterWrapper

func FromMap

func FromMap(m any) *IterWrapper

func WrapIterator

func WrapIterator(iter types.Iterator) *IterWrapper

func (*IterWrapper) Concat

func (it *IterWrapper) Concat(other types.Iterator) *IterWrapper

func (*IterWrapper) Enumerate

func (it *IterWrapper) Enumerate() *IterWrapper

func (*IterWrapper) Filter

func (it *IterWrapper) Filter(fn func(any) bool) *IterWrapper

func (*IterWrapper) ForEach

func (it *IterWrapper) ForEach(fn func(any))

func (*IterWrapper) Inspect

func (it *IterWrapper) Inspect(fn func(any)) *IterWrapper

func (*IterWrapper) Intersperse

func (it *IterWrapper) Intersperse(separator any) *IterWrapper

func (*IterWrapper) IntersperseWith

func (it *IterWrapper) IntersperseWith(separator func() any) *IterWrapper

func (*IterWrapper) Map

func (it *IterWrapper) Map(fn func(any) any) *IterWrapper

func (*IterWrapper) Next

func (it *IterWrapper) Next() (any, bool)

func (*IterWrapper) Reduce

func (it *IterWrapper) Reduce(base any, fn func(any, any) any) any

func (*IterWrapper) Scan

func (it *IterWrapper) Scan(base any, fn func(any, any) any) *IterWrapper

func (*IterWrapper) StepBy

func (it *IterWrapper) StepBy(step uint) *IterWrapper

func (*IterWrapper) Take

func (it *IterWrapper) Take(count int) *IterWrapper

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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