iter is a library of lazy iterators for Go. Built on pure functions, no unnecessary allocations, with full generics support and a clean functional style.
Apply transformations like Map, Filter, Take, Fold, Zip directly to sequences — lazily and efficiently.
go get github.com/enetx/iterpackage main
import (
"fmt"
"github.com/enetx/iter"
)
func main() {
// Extract even numbers and square them — chained, Rust-style:
out := iter.FromSlice([]int{1, 2, 3, 4, 5}).
Filter(func(x int) bool { return x%2 == 0 }). // 2, 4
Map(func(x int) int { return x * x }). // 4, 16
ToSlice()
fmt.Println(out) // [4 16]
}Requires Go 1.27+: transformations are generic methods, so Map, FilterMap, Scan,
Fold, Zip can change element types right in the chain.
- ✅ Lazy sequences — transformations are not applied until iteration begins.
- ✅ Generics-powered — works with any types
T,U, structs, etc. - ✅ Zero-allocation — avoids unnecessary memory allocations.
- ✅ Chainable methods — every transformation is a method:
Map,Filter,Take,Skip,StepBy,Zip,Enumerate,Fold,Reduce, and many more. Type-changing transformations are generic methods (Go 1.27). - ✅ Supports pairs, maps, channels —
Seq2with the same method set (no*2suffixes),FromMap,FromChan, and beyond. - ✅ Free functions where methods can't go — element-type constraints (
Dedup,Contains,Counter,ToMap) and slice-of-element results (Windows,Chunks,Flatten,Combinations,Permutations).
// Simple range with step
r := iter.Iota(1, 10).StepBy(3) // 1, 4, 7
fmt.Println(r.ToSlice()) // → [1 4 7]
// Iterate over a map
m := map[string]int{"a": 1, "b": 2}
iter.FromMap(m).ForEach(func(k string, v int) {
fmt.Printf("%s → %d\n", k, v)
})MIT
