ranges

package
v0.35.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2026 License: Apache-2.0 Imports: 1 Imported by: 2

README

ranges

Composable combinators over Go 1.23 iter.Seq[T] range-over-func iterators: Values, Empty, Filter/FilterPointer, Map, Join, Limit, Unique, and Slice. Part of rosetta.

This is the modern iteration toolkit (built on iter.Seq); for the older cursor-style Next(any) bool interface, see iterator.

What matters here

  • Every combinator propagates early termination via the yield return value. Each wraps the source sequence and returns from its own yield loop when the downstream yield returns false. This is what makes for x := range ranges.Limit(10, seq) stop the source after 10 items instead of draining it — break in the consumer must reach the producer. The tests assert this (*_EarlyTermination); a combinator that ignores yield's bool would silently break it.
  • iter.Seq values are lazy and re-runnable. Ranging the same sequence twice re-executes it from the start; combinators build a pipeline that does no work until ranged. Don't assume single-shot semantics.
  • FilterPointer passes *T to its predicate (to avoid copying large elements), while Filter passes T by value. Pick FilterPointer for big structs; the predicate must not retain the pointer past the call.
  • Slice is the terminal that materializes a sequence into []T — use it when you need a concrete slice; otherwise keep things lazy.

Documentation

Overview

Package ranges provides combinators for iter.Seq iterators: Filter, Map, Limit, Join, Unique, Empty, and Values, plus Slice to collect one.

Everything here is lazy. Each combinator returns a new iter.Seq that pulls from its source only as the consumer ranges over it, so chaining them composes a single pass rather than building intermediate slices, and stopping early stops the whole chain.

This is the modern counterpart to the iterator package, which adapts the older cursor-style interface. New code should start here.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Empty added in v0.25.28

func Empty[T any]() iter.Seq[T]

Empty returns an empty RangeFunc of any type. It yields no values.

func Filter added in v0.25.28

func Filter[T any](iterator iter.Seq[T], predicate func(T) bool) iter.Seq[T]

Filter returns a new iterator that yields only the items from the input iterator for which the predicate function returns true.

func FilterPointer added in v0.25.34

func FilterPointer[T any](iterator iter.Seq[T], predicate func(*T) bool) iter.Seq[T]

FilterPointer returns a new iterator that yields only the items from the input iterator for which the predicate function returns true.

func Join added in v0.25.28

func Join[T any](iterators ...iter.Seq[T]) iter.Seq[T]

Join combines multiple iterators into a single iterator that yields all items from each input iterator in sequence.

func Limit

func Limit[T any](max int, iterator iter.Seq[T]) iter.Seq[T]

Limit limits the number of items returned by an iter.Seq to the specified maximum

func Map added in v0.25.28

func Map[IN any, OUT any](iterator iter.Seq[IN], transform func(IN) OUT) iter.Seq[OUT]

Map transforms a rangeFunc of type T into a rangeFunc of type U using the provided transform function.

func Slice added in v0.25.20

func Slice[T any](rangeFunc iter.Seq[T]) []T

Slice converts an iter.Seq to a slice of T

func Unique added in v0.25.29

func Unique[T comparable](fn iter.Seq[T]) iter.Seq[T]

Unique returns an iterator that yields only unique items from the provided iterator.

func Values added in v0.25.28

func Values[T any](values ...T) iter.Seq[T]

Values returns a new iterator that yields the provided values

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL