Skip to content

v2.0.0

Choose a tag to compare

@mowshon mowshon released this 25 May 12:30
· 2 commits to main since this release
a9612c4

Iterium v2.0.0 is a major migration from Iterium's custom channel-backed iterator interface to Go's official iter.Seq and iter.Seq2 APIs.

This release makes Iterium feel like regular Go: sequences are consumed with for range, compose without hidden goroutines, stop early through the standard yield contract, and interoperate with iter.Pull when pull-style iteration is needed.

Highlights

  • Replaced the v1 Iter[T] interface with standard iter.Seq[T] values across the public API.
  • Added iter.Seq2[A, B] support with Product2, plus Chan2 and Slice2 adapters.
  • Removed default channel allocation and goroutine usage from normal iterator pipelines.
  • Added explicit adapters for boundaries: Chan, Chan2, Slice, SliceN, SliceUntil, and Slice2.
  • Added count helpers for sizing combinatoric work before iteration.
  • Added performance-oriented Into APIs that reuse result buffers for hot combinatoric loops.
  • Refreshed README, examples, benchmarks, and migration documentation for the Go iter API.

Breaking Changes

Public API now returns iter.Seq and iter.Seq2

Most Iterium functions now return standard Go iterator sequences:

for value := range iterium.Range(0, 10, 2) {
	fmt.Println(value)
}

The old v1 style no longer applies:

values := iterium.Range(5)
value, err := values.Next()

Removed v1 iterator interface and methods

The following v1 APIs were removed:

  • Iter[T]
  • IterRecover
  • .Next()
  • .Chan()
  • .Slice()
  • .Close()
  • .Count()
  • IsInfinite
  • SetInfinite

Use for range for normal consumption, iter.Pull for explicit pull-style iteration, and the top-level adapters when a slice or channel is required.

Channel conversion is now explicit and context-aware

v1:

for value := range iterium.Range(5).Chan() {
	fmt.Println(value)
}

v2:

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

for value := range iterium.Chan(ctx, iterium.Range(5)) {
	fmt.Println(value)
}

The context lets the producer stop when a channel consumer exits early.

Slice collection is now a top-level adapter

v1:

values, err := iterium.Range(5).Slice()

v2:

values := iterium.Slice(iterium.Range(5))

For unbounded or unknown-length sequences, use bounded collection helpers:

first := iterium.SliceN(iterium.Count[int](), 5)
until := iterium.SliceUntil(iterium.Count[int](), func(value int) bool {
	return value == 5
})

Infinite metadata was removed

iter.Seq does not carry metadata, so Iterium no longer tracks whether a sequence is infinite. Callers now own their bounds:

values := iterium.SliceN(iterium.Repeat("A", -1), 3)

Counts moved to explicit helpers

Iterators no longer have .Count(). Use the relevant count helper instead:

total := iterium.ProductCount(26, 4)
total, ok := iterium.CombinationsCountOK(26, 5)

The non-OK count helpers saturate at math.MaxInt64 on overflow. The *CountOK helpers return ok == false when the count cannot be represented.

New And Updated APIs

Sources and transforms

The core iterator helpers now return reusable standard sequences:

  • New(values...)
  • Empty[T]()
  • Range(args...)
  • Count(args...)
  • Repeat(value, n)
  • Map(seq, fn)
  • Filter(seq, pred)
  • FilterFalse(seq, pred)
  • Accumulate(seq, op)
  • TakeWhile(seq, pred)
  • DropWhile(seq, pred)
  • StarMap(seq, fn)
  • Cycle(seq)
  • FirstTrue(seq, pred)
  • FirstFalse(seq, pred)

StarMap is binary in v2.0.0 and expects each yielded slice to contain at least two values.

iter.Seq2 support

Product2 returns a two-value sequence:

for region, tier := range iterium.Product2(
	[]string{"us-east", "eu-west"},
	[]int{1, 2},
) {
	fmt.Println(region, tier)
}

Use Slice2 to collect iter.Seq2[A, B] into []Pair[A, B], or Chan2 to adapt it to a channel of pairs.

Count helpers

Use count helpers to validate expensive work before iterating:

  • RangeCount(start, stop, step)
  • ProductCount(countOfSymbols, repeat)
  • ProductCountOK(countOfSymbols, repeat)
  • CombinationsCount(n, k)
  • CombinationsCountOK(n, k)
  • CombinationsWithReplacementCount(n, k)
  • CombinationsWithReplacementCountOK(n, k)
  • PermutationCount(countOfSymbols, limit)
  • PermutationCountOK(countOfSymbols, limit)

Reused-buffer Into APIs

The normal combinatoric sequence APIs yield slices that are safe to keep. For allocation-sensitive loops, v2 adds callback APIs that reuse one result buffer:

  • ProductInto
  • ProductBytesInto
  • ProductStringInto
  • ProductRunesInto
  • CombinationsInto
  • CombinationsWithReplacementInto
  • PermutationsInto

Copy values inside the callback only if you need to keep them after the next yield.

Byte and rune alphabets

In addition to the existing string alphabets, v2 includes byte and rune variants for lower-allocation code:

  • AsciiLowercaseBytes, AsciiUppercaseBytes, AsciiLettersBytes
  • DigitsBytes, HexDigitsBytes, OctDigitsBytes
  • AsciiLowercaseRunes, AsciiUppercaseRunes, AsciiLettersRunes
  • DigitsRunes, HexDigitsRunes, OctDigitsRunes

Migration Cheat Sheet

v1 v2
iter.Next() for value := range seq or iter.Pull(seq)
iter.Chan() iterium.Chan(ctx, seq)
iter.Slice() iterium.Slice(seq)
iter.Count() ProductCount, CombinationsCount, PermutationCount, etc.
iter.IsInfinite() Bound consumption with SliceN, SliceUntil, TakeWhile, FirstTrue, FirstFalse, or break

The module path is unchanged:

import "github.com/mowshon/iterium"

Iterium v2 requires Go 1.23.4 or newer.

For a fuller migration guide with before-and-after examples, see MIGRATION.md.