xiter is an extension toolkit for Go's standard iter package. It provides functional, lazy, and type-safe sequence operations for both iter.Seq[E] and iter.Seq2[K, V].
- Features
- Installation
- Quick Start
- Core Concepts
- API Overview
- Best Practices
- Development & Testing
- Roadmap
- License
- ✅ Supports both
iter.Seq[E]anditer.Seq2[K, V] - ✅ Fully generic and type-safe
- ✅ Lazy evaluation with on-demand consumption
- ✅ Covers common workflows: map, filter, slice, reduce, search, compare, sorted checks
- ✅ Provides both functional APIs (
xiter) and fluent APIs (xiter/stream)
go get github.com/go-board/xiterpackage main
import (
"fmt"
"github.com/go-board/xiter"
)
func main() {
numbers := xiter.Range1(10) // 0..9
evens := xiter.Filter(numbers, func(v int) bool { return v%2 == 0 })
doubled := xiter.Map(evens, func(v int) int { return v * 2 })
sum := xiter.Fold(doubled, 0, func(acc, v int) int { return acc + v })
fmt.Println(sum) // 40
}
streamprovides a fluent wrapper (Stream/Stream2) for common workflows. It intentionally exposes a subset ofxiterAPIs rather than a 1:1 surface.
package main
import (
"fmt"
"github.com/go-board/xiter"
"github.com/go-board/xiter/stream"
)
func main() {
s := stream.Of(xiter.Range1(10)).
Skip(2).
Take(5).
Filter(func(v int) bool { return v%2 == 0 }).
Map(func(v int) int { return v * 10 })
result := make([]int, 0)
s.ForEach(func(v int) { result = append(result, v) })
fmt.Println(result) // [20 40 60]
}iter.Seq[E]: sequence of single valuesiter.Seq2[K, V]: sequence of key/value pairs- Sequences are lazy: execution happens at terminal stages like
ForEach,Fold,First, andLast - Sequences are usually single-pass: avoid re-consuming the same exhausted source
See full signatures on GoDoc: https://pkg.go.dev/github.com/go-board/xiter
Range1,Range2,Range3FromFunc,FromFunc2Once,Once2Empty,Empty2Repeat,Repeat2
Map,Map2MapWhile,MapWhile2FlatMap,FlattenEnumerateJoin,SplitCast
Filter,Filter2FilterMap,FilterMap2Take,Take2,TakeWhile,TakeWhile2Skip,Skip2,SkipWhile,SkipWhile2Chain,Chain2
ForEach,ForEach2Fold,Fold2Size,Size2,SizeFunc,SizeFunc2,SizeValue,SizeValue2
Contains,Contains2,ContainsFunc,ContainsFunc2Any,Any2,All,All2First,First2,FirstFunc,FirstFunc2Last,Last2,LastFunc,LastFunc2Position,Position2Compare,Compare2,CompareFunc,CompareFunc2Equal,Equal2,EqualFunc,EqualFunc2Max,MaxFunc,Min,MinFuncIsSorted,IsSortedFunc
- Compose transformations as pipelines for readability.
- Delay materialization (e.g. slice/map conversion) whenever possible.
- Always pair infinite sources (like
Repeat) with limiting operators (Take, etc.). - Use
*_Funcvariants for custom comparison and matching logic.
go test ./...- Add more examples and benchmarks
- Continue improving
streamAPI coverage and documentation
Apache-2.0. See LICENSE for details.