Documentation
¶
Overview ¶
Package stream provides a chainable collection operations library for Go.
Stream[T] wraps an iter.Seq[T] and provides lazy evaluation with method chaining for filter, sort, take, skip, and other operations that preserve the element type. For type-changing operations (Map, FlatMap, Zip), use top-level functions.
All operations are lazy by default. Operations that require full data (Sort, Reverse, Shuffle, TakeLast, Chunk, Partition) buffer internally and resume lazy iteration.
Usage:
// Method chaining for same-type operations
result := stream.Of(1, 2, 3, 4, 5).
Filter(func(n int) bool { return n%2 == 0 }).
Sort(func(a, b int) int { return a - b }).
Take(3).
ToSlice()
// Top-level functions for type-changing operations
names := stream.Map(
stream.Of(users...).Filter(func(u User) bool { return u.IsActive }),
func(u User) string { return u.Name },
).ToSlice()
Example (Chaining) ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
// Method chaining: filter even numbers, sort, take top 3
result := stream.Of(9, 2, 7, 4, 1, 8, 3, 6, 5).
Filter(func(n int) bool { return n%2 == 0 }).
Sort(func(a, b int) int { return a - b }).
Take(3).
ToSlice()
fmt.Println(result)
}
Output: [2 4 6]
Example (Fibonacci) ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
// Fibonacci sequence using Iterate with Pair
fib := stream.Map(
stream.Iterate(
stream.Pair[int, int]{First: 0, Second: 1},
func(p stream.Pair[int, int]) stream.Pair[int, int] {
return stream.Pair[int, int]{First: p.Second, Second: p.First + p.Second}
},
).Take(10),
func(p stream.Pair[int, int]) int { return p.First },
).ToSlice()
fmt.Println(fib)
}
Output: [0 1 1 2 3 5 8 13 21 34]
Example (LazyFilterTake) ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
// Lazy evaluation: only evaluates elements as needed
result := stream.Naturals().
Filter(func(n int) bool { return n%2 == 0 }).
Take(5).
ToSlice()
fmt.Println(result)
}
Output: [0 2 4 6 8]
Example (TextProcessing) ¶
package main
import (
"fmt"
"strings"
"github.com/nd-forge/stream"
)
func main() {
words := stream.Of("hello", "world", "hello", "go", "stream", "go").
Distinct(func(s string) string { return s }).
Sort(func(a, b string) int { return len(a) - len(b) })
result := stream.Map(words, strings.ToUpper).ToSlice()
fmt.Println(result)
}
Output: [GO HELLO WORLD STREAM]
Index ¶
- func Associate[T any, K comparable, V any](s Stream[T], fn func(T) (K, V)) map[K]V
- func Avg[T Number](s Stream[T]) float64
- func AvgBy[T any, N Number](s Stream[T], fn func(T) N) float64
- func GroupBy[T any, K comparable](s Stream[T], key func(T) K) map[K][]T
- func Max[T Number](s Stream[T]) (T, bool)
- func Min[T Number](s Stream[T]) (T, bool)
- func Reduce[T, U any](s Stream[T], initial U, fn func(acc U, item T) U) U
- func Sum[T Number](s Stream[T]) T
- func SumBy[T any, N Number](s Stream[T], fn func(T) N) N
- func ToMap[K comparable, V any](s Stream[Pair[K, V]]) map[K]V
- type Number
- type Pair
- type Stream
- func Collect[T any](seq iter.Seq[T]) Stream[T]
- func Collect2[K, V any](seq iter.Seq2[K, V]) Stream[Pair[K, V]]
- func Enumerate[T any](s Stream[T]) Stream[Pair[int, T]]
- func FlatMap[T, U any](s Stream[T], fn func(T) []U) Stream[U]
- func Flatten[T any](s Stream[[]T]) Stream[T]
- func From[T any](items []T) Stream[T]
- func Generate[T any](n int, gen func(index int) T) Stream[T]
- func Iterate[T any](seed T, fn func(T) T) Stream[T]
- func Map[T, U any](s Stream[T], fn func(T) U) Stream[U]
- func MapIndexed[T, U any](s Stream[T], fn func(int, T) U) Stream[U]
- func Naturals() Stream[int]
- func Of[T any](items ...T) Stream[T]
- func Range(start, end int) Stream[int]
- func Repeat[T any](value T) Stream[T]
- func RepeatN[T any](value T, n int) Stream[T]
- func Zip[T, U any](s1 Stream[T], s2 Stream[U]) Stream[Pair[T, U]]
- func (s Stream[T]) All(predicate func(T) bool) bool
- func (s Stream[T]) Any(predicate func(T) bool) bool
- func (s Stream[T]) Chain(others ...Stream[T]) Stream[T]
- func (s Stream[T]) Chunk(size int) []Stream[T]
- func (s Stream[T]) Contains(predicate func(T) bool) bool
- func (s Stream[T]) Count() int
- func (s Stream[T]) CountBy(predicate func(T) bool) int
- func (s Stream[T]) Distinct(key func(T) string) Stream[T]
- func (s Stream[T]) DropWhile(predicate func(T) bool) Stream[T]
- func (s Stream[T]) Filter(predicate func(T) bool) Stream[T]
- func (s Stream[T]) Find(predicate func(T) bool) (T, bool)
- func (s Stream[T]) First() (T, bool)
- func (s Stream[T]) ForEach(fn func(T))
- func (s Stream[T]) ForEachIndexed(fn func(int, T))
- func (s Stream[T]) IsEmpty() bool
- func (s Stream[T]) Last() (T, bool)
- func (s Stream[T]) MaxBy(less func(a, b T) bool) (T, bool)
- func (s Stream[T]) MinBy(less func(a, b T) bool) (T, bool)
- func (s Stream[T]) None(predicate func(T) bool) bool
- func (s Stream[T]) Partition(predicate func(T) bool) (matched Stream[T], unmatched Stream[T])
- func (s Stream[T]) Peek(fn func(T)) Stream[T]
- func (s Stream[T]) Reduce(initial T, fn func(acc, item T) T) T
- func (s Stream[T]) Reject(predicate func(T) bool) Stream[T]
- func (s Stream[T]) Reverse() Stream[T]
- func (s Stream[T]) Seq() iter.Seq[T]
- func (s Stream[T]) Shuffle() Stream[T]
- func (s Stream[T]) Skip(n int) Stream[T]
- func (s Stream[T]) Sort(cmp func(a, b T) int) Stream[T]
- func (s Stream[T]) Take(n int) Stream[T]
- func (s Stream[T]) TakeLast(n int) Stream[T]
- func (s Stream[T]) TakeWhile(predicate func(T) bool) Stream[T]
- func (s Stream[T]) ToSlice() []T
Examples ¶
- Package (Chaining)
- Package (Fibonacci)
- Package (LazyFilterTake)
- Package (TextProcessing)
- Avg
- Collect
- Collect2
- Enumerate
- FlatMap
- Flatten
- From
- Generate
- Iterate
- Map
- MapIndexed
- Max
- Min
- Naturals
- Of
- Range
- Reduce
- Repeat
- RepeatN
- Stream.All
- Stream.Any
- Stream.Chain
- Stream.Chunk
- Stream.Contains
- Stream.Count
- Stream.CountBy
- Stream.Distinct
- Stream.DropWhile
- Stream.Filter
- Stream.Find
- Stream.First
- Stream.ForEach
- Stream.ForEachIndexed
- Stream.IsEmpty
- Stream.Last
- Stream.MaxBy
- Stream.MinBy
- Stream.None
- Stream.Partition
- Stream.Peek
- Stream.Reduce
- Stream.Reject
- Stream.Reverse
- Stream.Seq
- Stream.Shuffle
- Stream.Skip
- Stream.Sort
- Stream.Take
- Stream.TakeLast
- Stream.TakeWhile
- Sum
- ToMap
- Zip
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Associate ¶
func Associate[T any, K comparable, V any](s Stream[T], fn func(T) (K, V)) map[K]V
Associate creates a map from Stream elements using a key-value function.
userMap := stream.Associate(users, func(u User) (int, string) {
return u.ID, u.Name
})
func Avg ¶
Avg returns the average of all elements in a numeric Stream.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
fmt.Println(stream.Avg(stream.Of(10.0, 20.0, 30.0)))
}
Output: 20
func GroupBy ¶
func GroupBy[T any, K comparable](s Stream[T], key func(T) K) map[K][]T
GroupBy groups elements by a key function and returns a map of key → slice.
bySymbol := stream.GroupBy(trades, func(t Trade) string { return t.Symbol })
// bySymbol["AUDUSD"] → []Trade
func Max ¶
Max returns the maximum element in a numeric Stream.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
v, _ := stream.Max(stream.Of(3, 1, 4, 1, 5))
fmt.Println(v)
}
Output: 5
func Min ¶
Min returns the minimum element in a numeric Stream.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
v, _ := stream.Min(stream.Of(3, 1, 4, 1, 5))
fmt.Println(v)
}
Output: 1
func Reduce ¶
Reduce folds all elements into a value of a different type.
total := stream.Reduce(orders, 0.0, func(acc float64, o Order) float64 {
return acc + o.Amount
})
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
sum := stream.Reduce(
stream.Of(1, 2, 3, 4, 5),
0,
func(acc, v int) int { return acc + v },
)
fmt.Println(sum)
}
Output: 15
func Sum ¶
Sum returns the sum of all elements in a numeric Stream.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
fmt.Println(stream.Sum(stream.Of(1, 2, 3, 4, 5)))
}
Output: 15
func ToMap ¶
func ToMap[K comparable, V any](s Stream[Pair[K, V]]) map[K]V
ToMap collects a Stream of Pairs into a map.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
m := stream.ToMap(stream.Zip(
stream.Of("a", "b", "c"),
stream.Of(1, 2, 3),
))
fmt.Println(m["a"], m["b"], m["c"])
}
Output: 1 2 3
Types ¶
type Number ¶
type Number interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
~float32 | ~float64
}
Number is a constraint for numeric types.
type Pair ¶
type Pair[T, U any] struct { First T Second U }
Pair holds two values of potentially different types.
type Stream ¶
type Stream[T any] struct { // contains filtered or unexported fields }
Stream is a lazy evaluation wrapper around iter.Seq[T] that supports method chaining. All intermediate operations are deferred until a terminal operation is called. Operations that require full data (Sort, Reverse, Shuffle, TakeLast) buffer internally then resume lazy iteration.
Stream is reusable: calling terminal operations multiple times produces the same result, as the underlying iter.Seq is re-executed each time.
func Collect ¶
Collect creates a Stream[T] from an iter.Seq[T].
// Use with standard library iterators keys := stream.Collect(maps.Keys(myMap)).Sort(cmp).ToSlice()
Example ¶
package main
import (
"fmt"
"maps"
"github.com/nd-forge/stream"
)
func main() {
m := map[string]int{"x": 1, "y": 2, "z": 3}
s := stream.Collect(maps.Values(m)).
Sort(func(a, b int) int { return a - b }).
ToSlice()
fmt.Println(s)
}
Output: [1 2 3]
func Collect2 ¶
Collect2 creates a Stream[Pair[K,V]] from an iter.Seq2[K,V].
pairs := stream.Collect2(maps.All(myMap))
Example ¶
package main
import (
"fmt"
"maps"
"github.com/nd-forge/stream"
)
func main() {
m := map[string]int{"a": 1}
pairs := stream.Collect2(maps.All(m)).ToSlice()
fmt.Println(pairs[0].First, pairs[0].Second)
}
Output: a 1
func Enumerate ¶
Enumerate wraps each element with its index as a Pair[int, T].
stream.Enumerate(stream.Of("a", "b", "c"))
// yields {0, "a"}, {1, "b"}, {2, "c"}
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Enumerate(stream.Of("a", "b", "c")).ToSlice()
for _, p := range result {
fmt.Printf("%d:%s ", p.First, p.Second)
}
fmt.Println()
}
Output: 0:a 1:b 2:c
func FlatMap ¶
FlatMap lazily transforms each element into a slice and flattens the result.
allOrders := stream.FlatMap(
stream.Of(users...),
func(u User) []Order { return u.Orders },
)
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.FlatMap(
stream.Of([]int{1, 2}, []int{3, 4}),
func(s []int) []int { return s },
).ToSlice()
fmt.Println(result)
}
Output: [1 2 3 4]
func Flatten ¶
Flatten lazily flattens a Stream of slices into a flat Stream.
flat := stream.Flatten(stream.Of([]int{1, 2}, []int{3, 4}))
// yields 1, 2, 3, 4
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Flatten(
stream.Of([]int{1, 2}, []int{3, 4}, []int{5}),
).ToSlice()
fmt.Println(result)
}
Output: [1 2 3 4 5]
func From ¶
From creates a new Stream from an existing slice (copies the slice).
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
data := []string{"Go", "Rust", "Python"}
result := stream.From(data).
Filter(func(s string) bool { return len(s) <= 4 }).
ToSlice()
fmt.Println(result)
}
Output: [Go Rust]
func Generate ¶
Generate creates a Stream of n elements using a generator function.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
squares := stream.Generate(5, func(i int) int { return i * i }).ToSlice()
fmt.Println(squares)
}
Output: [0 1 4 9 16]
func Iterate ¶
Iterate creates an infinite Stream: seed, fn(seed), fn(fn(seed)), ...
// Powers of 2: 1, 2, 4, 8, 16, ...
stream.Iterate(1, func(n int) int { return n * 2 }).Take(5)
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
// Powers of 2: 1, 2, 4, 8, 16
result := stream.Iterate(1, func(n int) int { return n * 2 }).
Take(5).ToSlice()
fmt.Println(result)
}
Output: [1 2 4 8 16]
func Map ¶
Map lazily transforms each element of type T into type U.
names := stream.Map(
stream.Of(users...).Filter(func(u User) bool { return u.IsActive }),
func(u User) string { return u.Name },
).ToSlice()
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Map(
stream.Of(1, 2, 3),
func(n int) string { return fmt.Sprintf("item_%d", n) },
).ToSlice()
fmt.Println(result)
}
Output: [item_1 item_2 item_3]
func MapIndexed ¶
MapIndexed is like Map but also provides the index to the transform function.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.MapIndexed(
stream.Of("a", "b", "c"),
func(i int, s string) string { return fmt.Sprintf("%d:%s", i, s) },
).ToSlice()
fmt.Println(result)
}
Output: [0:a 1:b 2:c]
func Naturals ¶
Naturals returns an infinite Stream of natural numbers: 0, 1, 2, 3, ...
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Naturals().Take(5).ToSlice()
fmt.Println(result)
}
Output: [0 1 2 3 4]
func Of ¶
Of creates a new Stream from variadic arguments.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Of(5, 2, 8, 1, 9).
Filter(func(n int) bool { return n%2 == 0 }).
Sort(func(a, b int) int { return a - b }).
ToSlice()
fmt.Println(result)
}
Output: [2 8]
func Range ¶
Range creates a Stream of integers from start (inclusive) to end (exclusive).
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Range(1, 6).ToSlice()
fmt.Println(result)
}
Output: [1 2 3 4 5]
func Repeat ¶
Repeat creates an infinite Stream that yields the same value. Must be combined with Take, TakeWhile, or Find to terminate.
zeros := stream.Repeat(0).Take(10).ToSlice()
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Repeat(42).Take(3).ToSlice()
fmt.Println(result)
}
Output: [42 42 42]
func RepeatN ¶
RepeatN creates a Stream that yields the same value n times.
dashes := stream.RepeatN("-", 20).ToSlice()
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.RepeatN("-", 4).ToSlice()
fmt.Println(result)
}
Output: [- - - -]
func Zip ¶
Zip lazily combines two Streams into a Stream of pairs. Stops when either Stream is exhausted.
pairs := stream.Zip(names, scores)
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
pairs := stream.Zip(
stream.Of("a", "b", "c"),
stream.Of(1, 2, 3),
).ToSlice()
for _, p := range pairs {
fmt.Printf("%s=%d ", p.First, p.Second)
}
fmt.Println()
}
Output: a=1 b=2 c=3
func (Stream[T]) All ¶
All returns true if all elements satisfy the predicate. Short-circuits on first non-match.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
fmt.Println(stream.Of(2, 4, 6).All(func(n int) bool { return n%2 == 0 }))
}
Output: true
func (Stream[T]) Any ¶
Any returns true if any element satisfies the predicate. Short-circuits on first match.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
fmt.Println(stream.Of(1, 2, 3).Any(func(n int) bool { return n > 2 }))
}
Output: true
func (Stream[T]) Chain ¶
Chain concatenates multiple Streams, yielding all elements from each in order.
combined := s1.Chain(s2, s3)
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
s1 := stream.Of(1, 2, 3)
s2 := stream.Of(4, 5, 6)
result := s1.Chain(s2).ToSlice()
fmt.Println(result)
}
Output: [1 2 3 4 5 6]
func (Stream[T]) Chunk ¶
Chunk collects all elements and splits them into chunks of the specified size. Note: This operation consumes all elements into memory.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
chunks := stream.Of(1, 2, 3, 4, 5).Chunk(2)
for _, c := range chunks {
fmt.Println(c.ToSlice())
}
}
Output: [1 2] [3 4] [5]
func (Stream[T]) Contains ¶
Contains returns true if any element satisfies the predicate.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
has := stream.Of(1, 2, 3).Contains(func(n int) bool { return n == 2 })
fmt.Println(has)
}
Output: true
func (Stream[T]) Count ¶
Count returns the total number of elements. Warning: Consumes the entire Stream. Do not use on infinite sequences.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
n := stream.Of(1, 2, 3).Count()
fmt.Println(n)
}
Output: 3
func (Stream[T]) CountBy ¶
CountBy returns the number of elements satisfying the predicate. Warning: Consumes the entire Stream. Do not use on infinite sequences.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
n := stream.Of(1, 2, 3, 4, 5).
CountBy(func(v int) bool { return v%2 == 0 })
fmt.Println(n)
}
Output: 2
func (Stream[T]) Distinct ¶
Distinct returns a Stream with duplicate elements removed. Uses the provided key function to determine equality. Note: Maintains a set of seen keys in memory.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Of("a", "b", "a", "c", "b").
Distinct(func(s string) string { return s }).
ToSlice()
fmt.Println(result)
}
Output: [a b c]
func (Stream[T]) DropWhile ¶
DropWhile skips elements from the start while the predicate is true, then yields the rest.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Of(1, 2, 3, 4, 5).
DropWhile(func(n int) bool { return n < 4 }).
ToSlice()
fmt.Println(result)
}
Output: [4 5]
func (Stream[T]) Filter ¶
Filter returns a Stream that yields only elements satisfying the predicate.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Of(1, 2, 3, 4, 5, 6).
Filter(func(n int) bool { return n%2 == 0 }).
ToSlice()
fmt.Println(result)
}
Output: [2 4 6]
func (Stream[T]) Find ¶
Find returns the first element matching the predicate. Short-circuits: stops iteration as soon as a match is found.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
v, ok := stream.Of(1, 2, 3, 4, 5).
Find(func(n int) bool { return n > 3 })
fmt.Println(v, ok)
}
Output: 4 true
func (Stream[T]) First ¶
First returns the first element and true, or zero value and false if empty. For infinite Streams, this returns immediately.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
v, ok := stream.Of(10, 20, 30).First()
fmt.Println(v, ok)
}
Output: 10 true
func (Stream[T]) ForEach ¶
func (s Stream[T]) ForEach(fn func(T))
ForEach executes a function for each element.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
stream.Of(1, 2, 3).ForEach(func(n int) {
fmt.Printf("%d ", n)
})
fmt.Println()
}
Output: 1 2 3
func (Stream[T]) ForEachIndexed ¶
ForEachIndexed executes a function for each element with its index.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
stream.Of("a", "b", "c").ForEachIndexed(func(i int, s string) {
fmt.Printf("%d:%s ", i, s)
})
fmt.Println()
}
Output: 0:a 1:b 2:c
func (Stream[T]) IsEmpty ¶
IsEmpty returns true if the Stream has no elements.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
fmt.Println(stream.Of[int]().IsEmpty())
fmt.Println(stream.Of(1).IsEmpty())
}
Output: true false
func (Stream[T]) Last ¶
Last returns the last element and true, or zero value and false if empty. Warning: This consumes the entire Stream. Do not use on infinite sequences.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
v, ok := stream.Of(10, 20, 30).Last()
fmt.Println(v, ok)
}
Output: 30 true
func (Stream[T]) MaxBy ¶
MaxBy returns the maximum element according to the comparison function. Warning: Consumes the entire Stream.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
v, _ := stream.Of(3, 1, 4, 1, 5).
MaxBy(func(a, b int) bool { return a < b })
fmt.Println(v)
}
Output: 5
func (Stream[T]) MinBy ¶
MinBy returns the minimum element according to the comparison function. Warning: Consumes the entire Stream.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
v, _ := stream.Of(3, 1, 4, 1, 5).
MinBy(func(a, b int) bool { return a < b })
fmt.Println(v)
}
Output: 1
func (Stream[T]) None ¶
None returns true if no elements satisfy the predicate.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
fmt.Println(stream.Of(1, 2, 3).None(func(n int) bool { return n > 10 }))
}
Output: true
func (Stream[T]) Partition ¶
Partition collects all elements and splits them into two Streams: elements that match the predicate and those that don't. Note: This operation consumes all elements into memory.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
evens, odds := stream.Of(1, 2, 3, 4, 5).
Partition(func(n int) bool { return n%2 == 0 })
fmt.Println("evens:", evens.ToSlice())
fmt.Println("odds:", odds.ToSlice())
}
Output: evens: [2 4] odds: [1 3 5]
func (Stream[T]) Peek ¶
Peek executes a side-effect function for each element without modifying the Stream. Useful for debugging or logging within a lazy chain.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
var log []string
stream.Of("hello", "world").
Peek(func(s string) { log = append(log, "saw:"+s) }).
ToSlice()
fmt.Println(log)
}
Output: [saw:hello saw:world]
func (Stream[T]) Reduce ¶
func (s Stream[T]) Reduce(initial T, fn func(acc, item T) T) T
Reduce folds all elements into a single value of the same type. For reducing to a different type, use the top-level Reduce function.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
sum := stream.Of(1, 2, 3, 4, 5).
Reduce(0, func(acc, v int) int { return acc + v })
fmt.Println(sum)
}
Output: 15
func (Stream[T]) Reject ¶
Reject returns a Stream that excludes elements satisfying the predicate.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Of(1, 2, 3, 4, 5).
Reject(func(n int) bool { return n%2 == 0 }).
ToSlice()
fmt.Println(result)
}
Output: [1 3 5]
func (Stream[T]) Reverse ¶
Reverse buffers all elements and yields them in reverse order. Note: This operation consumes all elements into memory.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Of(1, 2, 3).Reverse().ToSlice()
fmt.Println(result)
}
Output: [3 2 1]
func (Stream[T]) Seq ¶
Seq returns the underlying iter.Seq[T]. Use this for interop with standard library functions like slices.Collect.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
var result []int
for v := range stream.Of(1, 2, 3).Seq() {
result = append(result, v)
}
fmt.Println(result)
}
Output: [1 2 3]
func (Stream[T]) Shuffle ¶
Shuffle buffers all elements, randomizes their order, and yields them. Note: This operation consumes all elements into memory.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
s := stream.Of(1, 2, 3, 4, 5).Shuffle()
// Shuffle randomizes order, so just check count
fmt.Println(s.Count())
}
Output: 5
func (Stream[T]) Skip ¶
Skip returns a Stream that skips the first n elements.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Of(10, 20, 30, 40, 50).Skip(2).ToSlice()
fmt.Println(result)
}
Output: [30 40 50]
func (Stream[T]) Sort ¶
Sort buffers all elements, sorts them, and yields in sorted order. Note: This operation consumes all elements into memory, breaking pure laziness. However, subsequent operations in the chain remain lazy.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Of(3, 1, 4, 1, 5).
Sort(func(a, b int) int { return a - b }).
ToSlice()
fmt.Println(result)
}
Output: [1 1 3 4 5]
func (Stream[T]) Take ¶
Take returns a Stream that yields only the first n elements. For infinite sequences, this is the primary way to limit output.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Of(10, 20, 30, 40, 50).Take(3).ToSlice()
fmt.Println(result)
}
Output: [10 20 30]
func (Stream[T]) TakeLast ¶
TakeLast buffers all elements and returns only the last n elements. Note: This operation consumes all elements into memory.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Of(10, 20, 30, 40, 50).TakeLast(2).ToSlice()
fmt.Println(result)
}
Output: [40 50]
func (Stream[T]) TakeWhile ¶
TakeWhile returns elements from the start as long as the predicate is true.
Example ¶
package main
import (
"fmt"
"github.com/nd-forge/stream"
)
func main() {
result := stream.Of(1, 2, 3, 4, 5).
TakeWhile(func(n int) bool { return n < 4 }).
ToSlice()
fmt.Println(result)
}
Output: [1 2 3]