ustream

package
v3.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

ustream

Stream-style helpers for fluent slice processing.

Use this package when a pipeline is clearer than direct uarray calls:

Example

result := ustream.From(5, 1, 2, 2, 3, 4).
	Filter(func(v int) bool {
		return v >= 2
	}).
	DistinctBy(func(v int) string {
		return strconv.Itoa(v)
	}).
	Transform(func(v int) int {
		return v * 10
	}).
	Sort(func(a, b int) bool {
		return a < b
	}).
	Skip(1).
	Limit(2).
	Reverse().
	Collect()

For same-type chains use methods: Filter, FilterOut, Transform, FlatMap, Peek, Limit, Skip, TakeWhile, DropWhile, Reverse, Sort, DistinctBy, CompactFunc, Find, FindIndex, AnyMatch, AllMatch, NoneMatch, Reduce, Collect.

For type-changing operations Go does not allow method-level type parameters, so use typed package functions:

stream := ustream.From(1, 2, 3)

mapped := ustream.Map(stream, func(v int) string {
	return fmt.Sprintf("n-%d", v)
})

grouped := ustream.Collect(mapped, ustream.GroupingBy(func(v string) int {
	return len(v)
}))

Collectors are available through Collect, ToSlice, ToSliceCopy, ToMap, ToMultiMap, GroupingBy, and CountingBy.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Collect

func Collect[T, R any](stream *Stream[T], collector ResultCollector[T, R]) R

Collect applies a typed collector to a stream.

func Reduce

func Reduce[T, R any](stream *Stream[T], initial R, reducer func(acc R, value T) R) R

Reduce folds stream values into any result type.

Types

type Collector

type Collector[T any] interface {
	Collect() []T
	CollectToMap(func(T) (any, any)) map[any]any
}

Collector defines the legacy interface for collecting elements from a stream.

type ResultCollector

type ResultCollector[T, R any] interface {
	Collect(values []T) R
}

ResultCollector collects stream values into a custom result.

func CountingBy

func CountingBy[T any, K comparable](key func(T) K) ResultCollector[T, map[K]int]

CountingBy creates a collector that counts stream values by key.

func GroupingBy

func GroupingBy[T any, K comparable](key func(T) K) ResultCollector[T, map[K][]T]

GroupingBy creates a collector that groups stream values by key.

func ToMap

func ToMap[T any, K comparable, R any](mapper func(T) (K, R)) ResultCollector[T, map[K]R]

ToMap creates a collector that maps stream values into a map.

func ToMultiMap

func ToMultiMap[T any, K comparable, R any](mapper func(T) (K, R)) ResultCollector[T, map[K][]R]

ToMultiMap creates a collector that maps stream values into grouped map values.

func ToSlice

func ToSlice[T any]() ResultCollector[T, []T]

ToSlice creates a collector that returns stream values.

func ToSliceCopy

func ToSliceCopy[T any]() ResultCollector[T, []T]

ToSliceCopy creates a collector that returns a copy of stream values.

type ResultCollectorFunc

type ResultCollectorFunc[T, R any] func(values []T) R

ResultCollectorFunc adapts a function to ResultCollector.

func (ResultCollectorFunc[T, R]) Collect

func (f ResultCollectorFunc[T, R]) Collect(values []T) R

Collect adapts a function to ResultCollector.

type Stream

type Stream[T any] struct {
	// contains filtered or unexported fields
}

Stream wraps a slice and exposes fluent collection operations.

func Compact

func Compact[T comparable](stream *Stream[T]) *Stream[T]

Compact removes zero values from a comparable stream.

func Distinct

func Distinct[T comparable](stream *Stream[T]) *Stream[T]

Distinct keeps the first occurrence of each comparable value.

func DistinctBy

func DistinctBy[T any, K comparable](stream *Stream[T], key func(T) K) *Stream[T]

DistinctBy keeps the first value for each comparable key.

func Empty

func Empty[T any]() *Stream[T]

Empty creates a new empty Stream.

func FlatMap

func FlatMap[T, R any](stream *Stream[T], mapper func(T) []R) *Stream[R]

FlatMap maps stream values into another type and flattens the result.

func From

func From[T any](values ...T) *Stream[T]

From creates a new Stream from variadic values.

func Generate

func Generate[T any](count int, supplier func(index int) T) *Stream[T]

Generate creates a Stream by calling supplier count times.

func Iterate

func Iterate[T any](seed T, count int, next func(T) T) *Stream[T]

Iterate creates a Stream by repeatedly applying next to the previous value.

func Map

func Map[T, R any](stream *Stream[T], mapper func(T) R) *Stream[R]

Map maps stream values into another type.

func Of

func Of[T any](values []T) *Stream[T]

Of creates a new Stream from the given slice.

func Sorted

func Sorted[T cmp.Ordered](stream *Stream[T]) *Stream[T]

Sorted sorts ordered stream values.

func (*Stream[T]) AllMatch

func (s *Stream[T]) AllMatch(predicate func(T) bool) bool

AllMatch checks whether all values match predicate.

func (*Stream[T]) AnyMatch

func (s *Stream[T]) AnyMatch(predicate func(T) bool) bool

AnyMatch checks whether any value matches predicate.

func (*Stream[T]) Collect

func (s *Stream[T]) Collect() []T

Collect returns stream values.

func (*Stream[T]) CollectCopy

func (s *Stream[T]) CollectCopy() []T

CollectCopy returns a copy of stream values.

func (*Stream[T]) CollectToMap

func (s *Stream[T]) CollectToMap(mapper func(T) (any, any)) map[any]any

CollectToMap collects stream values into a map. Duplicate keys are overwritten.

func (*Stream[T]) CollectToMultiMap

func (s *Stream[T]) CollectToMultiMap(mapper func(T) (any, any)) map[any][]any

CollectToMultiMap collects stream values into a grouped map.

func (*Stream[T]) CollectWith

func (s *Stream[T]) CollectWith(collector func([]T) any) any

CollectWith collects stream values using a custom collector.

func (*Stream[T]) CompactFunc

func (s *Stream[T]) CompactFunc(empty func(T) bool) *Stream[T]

CompactFunc removes values matching empty.

func (*Stream[T]) Count

func (s *Stream[T]) Count() int

Count returns stream size.

func (*Stream[T]) DistinctBy

func (s *Stream[T]) DistinctBy(key func(T) string) *Stream[T]

DistinctBy keeps the first value for each string key.

func (*Stream[T]) DropWhile

func (s *Stream[T]) DropWhile(predicate func(T) bool) *Stream[T]

DropWhile skips leading values while predicate returns true.

func (*Stream[T]) Filter

func (s *Stream[T]) Filter(predicate func(T) bool) *Stream[T]

Filter keeps values matching predicate.

func (*Stream[T]) FilterOut

func (s *Stream[T]) FilterOut(predicate func(T) bool) *Stream[T]

FilterOut removes values matching predicate.

func (*Stream[T]) Find

func (s *Stream[T]) Find(predicate func(T) bool) *T

Find returns the first value matching predicate.

func (*Stream[T]) FindIndex

func (s *Stream[T]) FindIndex(predicate func(T) bool) int

FindIndex returns the first index matching predicate, or -1.

func (*Stream[T]) FlatMap

func (s *Stream[T]) FlatMap(mapper func(T) []T) *Stream[T]

FlatMap expands each value into zero or more values of the same type.

func (*Stream[T]) ForEach

func (s *Stream[T]) ForEach(action func(T))

ForEach executes action for each value.

func (*Stream[T]) Limit

func (s *Stream[T]) Limit(n int) *Stream[T]

Limit keeps at most n first values.

func (*Stream[T]) Map

func (s *Stream[T]) Map(mapper func(T) any) *TerminalStream[any]

Map maps values into any and returns a terminal stream for legacy compatibility.

func (*Stream[T]) NoneMatch

func (s *Stream[T]) NoneMatch(predicate func(T) bool) bool

NoneMatch checks whether no values match predicate.

func (*Stream[T]) Peek

func (s *Stream[T]) Peek(action func(T)) *Stream[T]

Peek executes action for each value and returns the same stream values.

func (*Stream[T]) Reduce

func (s *Stream[T]) Reduce(initial T, reducer func(acc T, value T) T) T

Reduce folds values into one value.

func (*Stream[T]) Reverse

func (s *Stream[T]) Reverse() *Stream[T]

Reverse returns a reversed copy of stream values.

func (*Stream[T]) Skip

func (s *Stream[T]) Skip(n int) *Stream[T]

Skip removes n first values.

func (*Stream[T]) Sort

func (s *Stream[T]) Sort(less func(a, b T) bool) *Stream[T]

Sort returns a sorted copy using less.

func (*Stream[T]) TakeWhile

func (s *Stream[T]) TakeWhile(predicate func(T) bool) *Stream[T]

TakeWhile keeps leading values while predicate returns true.

func (*Stream[T]) ToTerminal

func (s *Stream[T]) ToTerminal() *TerminalStream[T]

ToTerminal converts a stream to a terminal stream.

func (*Stream[T]) Transform

func (s *Stream[T]) Transform(mapper func(T) T) *Stream[T]

Transform maps values without changing their type.

type TerminalStream

type TerminalStream[T any] struct {
	// contains filtered or unexported fields
}

TerminalStream represents a stream that can only be collected or executed.

func NewTerminalStream

func NewTerminalStream[T any](values []T) *TerminalStream[T]

NewTerminalStream creates a terminal stream from values.

func (*TerminalStream[T]) Collect

func (s *TerminalStream[T]) Collect() []T

Collect returns terminal stream values.

func (*TerminalStream[T]) CollectCopy

func (s *TerminalStream[T]) CollectCopy() []T

CollectCopy returns a copy of terminal stream values.

func (*TerminalStream[T]) CollectToMap

func (s *TerminalStream[T]) CollectToMap(mapper func(T) (any, T)) map[any][]T

CollectToMap collects terminal stream values into a multimap.

func (*TerminalStream[T]) ParallelExecute

func (s *TerminalStream[T]) ParallelExecute(fn func(int, *T), parallelism int)

ParallelExecute executes fn concurrently on each stream value.

func (*TerminalStream[T]) ParallelExecuteWithTimeout

func (s *TerminalStream[T]) ParallelExecuteWithTimeout(fn func(int, T), cancel func(int, T), timeout time.Duration, parallelism int)

ParallelExecuteWithTimeout executes fn concurrently and calls cancel when an item exceeds timeout.

Jump to

Keyboard shortcuts

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