Documentation
¶
Overview ¶
Package stream provides Java Streams-like functional operations on Go collections.
It enables true lazy evaluation, parallel processing, and functional-style pipelines using Go generics and the iter package (requires Go 1.23+).
Intermediate operations compose iter.Seq[T] closures without executing any work. Processing is deferred until a terminal operation ranges over the pipeline. Short-circuit operations (First, AnyMatch, Limit) naturally stop early.
Quick Start ¶
Create a stream from a slice, apply intermediate operations, and collect results:
sum := stream.SliceOf(1, 2, 3, 4, 5).
Filter(func(n int) bool { return n%2 == 1 }).
Map(func(n int) int { return n * n }).
Reduce(func(a, b int) int { return a + b })
// sum == 35
Stream Creation ¶
Use factory functions to create streams:
- SliceOf: from a slice or variadic elements
- From: from an iter.Seq[T] (supports infinite streams)
- From2: from an iter.Seq2[K, V]
- Repeat: infinite repeating element
- RepeatN: element repeated N times
- Concat: combine multiple streams
Operations ¶
Intermediate (lazy, return a new stream):
- Stateless: Filter, Map, Convert, Peek, FlatMap
- Stateful: Distinct, Sort, ReverseSort, Reverse, Limit, Skip, Pick
Terminal (eager, execute the pipeline):
- Collect: ToSlice, Collect
- Iterate: ForEach, Seq (native iter.Seq[T] for range loops)
- Reduce: Reduce, ReduceFrom, ReduceWith, ReduceBy
- Match: AllMatch, NonMatch, AnyMatch
- Element: First, Take, Any, Last
- Count: Count
iter.Seq Integration ¶
The Seq() method returns the underlying iter.Seq[T] for use with Go's range:
for v := range stream.SliceOf(1, 2, 3).Seq() {
fmt.Println(v)
}
Parallel Processing ¶
Use Parallel(n) to enable concurrent processing. n controls concurrency:
0: no change (synchronous)
1+: concurrent workers
stream.SliceOf(data...).Parallel(4).Filter(...).ForEach(...)
Helper Functions ¶
- To[T, R]: converts a slice of T to a slice of R via a converter
- AnyTo[T]: converts []any to []T via type assertion
Important ¶
Streams are single-use. Each terminal operation consumes the underlying iterator. Create a new stream for each pipeline.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnsupportType unsupport type ErrUnsupportType = errors.New("unsupport type") )
Functions ¶
Types ¶
type Sortable ¶
type Sortable[T any] struct { List []T Cmp types.Comparator[T] }
type Streamer ¶
type Streamer[T any] interface { // WithContext set Streamer context WithContext(context.Context) Streamer[T] // Filter filter data by Judge result Filter(types.Judge[T]) Streamer[T] Map(types.Mapper[T]) Streamer[T] Convert(types.Converter[T, any]) Streamer[any] Peek(types.Consumer[T]) Streamer[T] // FlatMap flattens each element to a sub-stream and concatenates FlatMap(func(T) Streamer[any]) Streamer[any] Distinct() Streamer[T] Sort(types.Comparator[T]) Streamer[T] ReverseSort(types.Comparator[T]) Streamer[T] Reverse() Streamer[T] Limit(int64) Streamer[T] Skip(int64) Streamer[T] Pick(startIndex, endIndex, interval int) Streamer[T] // Append append data to streamer source Append(...T) Streamer[T] // Execute eager execute streamer stage Execute() Streamer[T] // Parallel 0 do nothing, 1 async work, 2-n concurrent work Parallel(int) Streamer[T] ToSlice() []T Collect(types.Collector[T]) any ForEach(types.Consumer[T]) // Match methods AllMatch(types.Judge[T]) bool NonMatch(types.Judge[T]) bool AnyMatch(types.Judge[T]) bool // Reduce reduce calculate Reduce(accumulator types.BinaryOperator[T]) T ReduceFrom(initValue T, accumulator types.BinaryOperator[T]) T ReduceWith(initValue any, accumulator types.Accumulator[T, any]) any ReduceBy(initValueBuilder func(sizeMayNegative int) any, accumulator types.Accumulator[T, any]) any // Pick one First() T Take() T Any() T Last() T // Count return count result Count() int64 // Seq returns the underlying iter.Seq[T] for native range loops Seq() iter.Seq[T] }