Iterator implementation for Golang to provide map and reduce functionalities.
import (
"github.com/yaa110/goterator"
"github.com/yaa110/goterator/generator"
)
- Create a generator from slices
words := []interface{}{"an", "example", "of", "goterator"}
gen := generator.NewSlice(words)
- Create a generator from channels
chn := make(chan interface{}, 4)
chn <- "an"
chn <- "example"
chn <- "of"
chn <- "goterator"
close(chn)
gen := generator.NewChannel(chn)
- Create custom generators
type TestGenerator struct {
words []string
i int
value string
}
func (g *TestGenerator) Next() bool {
if g.i == len(g.words) {
return false
}
g.value = g.words[g.i]
g.i++
return true
}
func (g *TestGenerator) Value() interface{} {
return g.value
}
gen := &TestGenerator{
words: []string{"an", "example", "of", "goterator"},
i: 0,
value: "",
}
- Iterate over generators
lengths := goterator.New(gen).Map(func(word interface{}) interface{} {
return len(word.(string))
}).Collect()
assert.Equal([]interface{}{2, 7, 2, 9}, lengths)
Please for more information about mappers and reducers (consumers) check the documentation.