# go-iterator **Repository Path**: xxbiji/go-iterator ## Basic Information - **Project Name**: go-iterator - **Description**: go实现的简单的iterator接口 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 5 - **Forks**: 0 - **Created**: 2022-10-25 - **Last Updated**: 2023-08-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 简单的go iterator接口 ## 特点 ### 链式调用 ```go 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函数 ```go 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`接口 ```go 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 将一个`array`或`slice`转换成`IterWrapper` ### FromMap func(any) IterWrapper 将一个`map`转换成`IterWrapper` ### consumer函数 #### Collect func[T any]() []T 将迭代器返回指定类型的数组,如果无法转换,则返回对应类型的零值 ```go iter := iterator.FromArray([]int{1, 2, 3, 4, 5}) consumer.Collect[int](iter) // 返回 []int{1, 2, 3, 4, 5} ```