Documentation ¶
Overview ¶
Package Stream defines a set of operations that provide lazy operations on a Stream of data values. This package is still experimental, please use at your own risk.
A Stream is an abstraction that produces a single value at a time. A Stream can produce infinite values, or just a finite set of values. A simple example of using a Stream is:
ForEach(Take(Iota[int](), 10), func(value int) { // Do something with 'value' })
Iota() produces a Stream, which is consumed by Take(), which produces a Stream that is consumed by ForEach().
Streams are synchronous by default. A segment of a Stream can be made asynchronous by using the Async() adapter. All operations inside this adapter will execute asynchronously from the main thread, and their results will be fed through a channel back to the main thread.
The design of streams is heavily influenced by C#'s LINQ and Java's Stream API. The current design is not considered stable yet, so newer versions may cause the API to change significantly.
Index ¶
- func ChunkOut[T any](input Stream[T], size int) [][]T
- func ForEach[T any](s Stream[T], op common.UnaryOp[T])
- func OneOrNone[T any](input Stream[T]) *cnt.Optional[T]
- func ToSlice[T any](input Stream[T]) []T
- type Chain
- func (s Chain[T]) Filter(pred common.Predicate[T]) Chain[T]
- func (s Chain[T]) ForEach(pred common.UnaryOp[T])
- func (s Chain[T]) MergeSort(o Chain[T], pred common.BinaryPredicate[T]) Chain[T]
- func (s Chain[T]) OneOrNone() *cnt.Optional[T]
- func (s Chain[T]) Skip(limit int) Chain[T]
- func (s Chain[T]) Take(limit int) Chain[T]
- func (s Chain[T]) ToSlice() []T
- type Stream
- func Async[T any](input Stream[T]) Stream[T]
- func ChunkIn[T any](input []T, size int) []Stream[T]
- func Filter[T any](input Stream[T], pred common.Predicate[T]) Stream[T]
- func FromSlice[T any](data []T) Stream[T]
- func Generate[T any](g func() *cnt.Optional[T]) Stream[T]
- func Iota[T constraints.Integer]() Stream[T]
- func IotaWithStart[T constraints.Integer](start T) Stream[T]
- func MergeSort[T any](input1, input2 Stream[T], pred common.BinaryPredicate[T]) Stream[T]
- func Skip[T any](input Stream[T], limit int) Stream[T]
- func Take[T any](input Stream[T], limit int) Stream[T]
- func Transform[T1, T2 any](input Stream[T1], xform common.UnaryTransform[T1, T2]) Stream[T2]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ChunkOut ¶ added in v0.0.4
ChunkOut drains a Stream and provides its output as an array of chunks.
func ForEach ¶
ForEach processes each item in the Stream. It will not stop until the input Stream terminates.
Types ¶
type Chain ¶ added in v0.0.7
func Monotonic ¶ added in v0.0.7
func Monotonic[T constraints.Integer]() Chain[T]
func MonotonicWithStart ¶ added in v0.0.7
func MonotonicWithStart[T constraints.Integer](start T) Chain[T]
type Stream ¶
type Stream[T any] interface { // Next retrieves the next item from the Stream. It returns an Optional[T]. // If the result is empty it means that the Stream has finished. Next() *cnt.Optional[T] }
Stream is an interface that provides a uni-directional, pull-based data processing facility.
func Async ¶ added in v0.0.4
Async creates a new asynchronous Stream. The input Stream will be read from a go routine, and its results transferred over a channel. That means whatever work the input Stream needs to do can be detached from the work happening in this thread.
func ChunkIn ¶ added in v0.0.4
ChunkIn provides an array of streams, each one draining a chunk of the input slice.
func FromSlice ¶ added in v0.0.4
FromSlice creates a Stream that provides all the elements in the slice, one at a time. When it reaches the end is terminates the Stream.
func Generate ¶ added in v0.0.4
Generate creates a Stream that generates user-defined values. When the generator wants to terminate the Stream, it should return true as it's second resut argument.
func Iota ¶
func Iota[T constraints.Integer]() Stream[T]
Iota creates a Stream that provides an infinite number of monotonically increasing integers.
func IotaWithStart ¶
func IotaWithStart[T constraints.Integer](start T) Stream[T]
IotaWithStart creates a Stream just like Iota, but allows you to set the starting point.
func MergeSort ¶ added in v0.0.4
func MergeSort[T any](input1, input2 Stream[T], pred common.BinaryPredicate[T]) Stream[T]
MergeSort creates a Stream that sorts the two streams. The pred function should return true if value1 should come before value2. This Stream step requires that both input streams are already sorted with respect to themselves.