https://github.com/mtoohey31/iter
Package iter provides generic, lazy iterators, functions for producing them from primitive types, as well as functions and methods for transforming and consuming them.
https://github.com/mtoohey31/iter
generic generics go golang iterator lazy
Last synced: 17 days ago
JSON representation
Package iter provides generic, lazy iterators, functions for producing them from primitive types, as well as functions and methods for transforming and consuming them.
- Host: GitHub
- URL: https://github.com/mtoohey31/iter
- Owner: mtoohey31
- License: bsd-3-clause
- Archived: true
- Created: 2022-01-07T22:16:51.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-08-25T16:53:07.000Z (over 1 year ago)
- Last Synced: 2025-07-07T06:08:06.362Z (7 months ago)
- Topics: generic, generics, go, golang, iterator, lazy
- Language: Go
- Homepage: https://pkg.go.dev/mtoohey.com/iter/v2
- Size: 333 KB
- Stars: 33
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
> As of Go 1.23, the [`for ... := range ...` syntax](https://go.dev/ref/spec#For_range) can now be used with "push-style" iterator functions. Since this package implements a different "pull-style" of iterators which are incompatible with the `for ... := range ...` syntax, I've decided to archive it. If you're looking for pull-style equivalents of some of the functions in this package, I'd recommend checking out the [`x/exp/iter` package proposal](https://github.com/golang/go/issues/61898).
# iter
Package iter provides generic, lazy iterators, functions for producing them from primitive types, as well as functions and methods for transforming and consuming them.
## Usage
```go
package main
import (
"fmt"
"strings"
"mtoohey.com/iter/v2"
)
func main() {
initial := iter.Elems([]string{"hello", "beautiful", "world"})
result := initial.Filter(func(s string) bool {
return len(s) < 6
}).Map(strings.ToUpper).Collect()
fmt.Println(result) // produces: [HELLO WORLD]
}
```
## Regarding Performance
There is some overhead to using the iterators in this package, since each evaluation requires a function call, so if the performance of your application is a top priority, this package _might_ not be the best choice. Don't guess about performance though: I would recommend benchmarking to determine the impact of using iterators, because in some cases, lazy iterators may be faster than the equivalent strict loop.
## Acknowledgements
- Everyone that commented on my [r/golang post](https://www.reddit.com/r/golang/comments/s13jlz/iter_generic_lazy_iterators_for_go_118/)
- The inspiration for my own attempt: