yaa110 / goterator

Lazy iterator implementation for Golang

Home Page:https://godoc.org/github.com/yaa110/goterator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Goterator

Test and Build PkgGoDev GoDoc Go Report Coverage

Iterator implementation for Golang to provide map and reduce functionalities.

Package

import (
    "github.com/yaa110/goterator"
    "github.com/yaa110/goterator/generator"
)

Getting Started

  • 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.

About

Lazy iterator implementation for Golang

https://godoc.org/github.com/yaa110/goterator

License:MIT License


Languages

Language:Go 100.0%