Skip to content

enetx/iter

Repository files navigation

GOPHER_ROCKS

iter — Lazy Iterators with Generics for Go

Go Reference Go Report Card Coverage Status Go Ask DeepWiki

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.


📦 Installation

go get github.com/enetx/iter

🚀 Example

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


✨ Features

  • 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, channelsSeq2 with the same method set (no *2 suffixes), 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).

🔬 More Examples

// 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)
})

⚖️ License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

2 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages