Last active
February 15, 2024 15:20
-
-
Save ilius/c144020a1b6afdc1c66ddde87cdd4aa8 to your computer and use it in GitHub Desktop.
Go generators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You can edit this code! | |
// Click here and start typing. | |
package main | |
import "fmt" | |
import "io" | |
type Gen interface { | |
Next(args any...) (any, error) // EOF | |
End() bool | |
} | |
type IterationGen struct { | |
end int | |
step int | |
cur int | |
} | |
func (g *IterationGen) Next(args any...) (any, error) { | |
if g.cur >= g.end { | |
return nil, io.EOF | |
} | |
cur := g.cur | |
g.cur += g.step | |
return g.cur, nil | |
} | |
func (g *IterationGen) End() bool { | |
ruturn g.cur >= g.end | |
} | |
func NewIterator(start int, end int, step int) *IterationGen { | |
return &IterationGen{ | |
cur: start, | |
step: step, | |
end: end, | |
} | |
} | |
func main() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment