stream

package
v2.0.1-0...-e727a46 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package stream provides a way to construct data processing streams from smaller pieces.

Index

Constants

This section is empty.

Variables

View Source
var ErrBreak = errors.New("break prematurely")
View Source
var ErrPullEnd = errors.New("pull end prematurely")

Functions

func Any

func Any[A any](xs Stream[A], p func(A) bool) bool

Any consumes the stream and checks if any of the stream elements matches the predicate.

func CollectCounter

func CollectCounter[A comparable](xs Stream[A]) fun.Counter[A]

CollectCounter consumes the stream makes Counter with count of how many times each element was seen.

func CollectToSet

func CollectToSet[A comparable](xs Stream[A]) fun.Set[A]

CollectToSet executes the stream and collects all results to a set.

func CollectToSlice

func CollectToSlice[A any](xs Stream[A]) []A

CollectToSlice executes the stream and collects all results to a slice.

func Count

func Count[A any](xs Stream[A]) int

Count consumes stream and returns it's length.

func Find

func Find[A any](xs Stream[A], p func(A) bool) fun.Option[A]

Find searches for first element matching the predicate.

func ForEach

func ForEach[A any](xs Stream[A], f func(A))

ForEach invokes a simple function for each element of the stream.

func Group

func Group[A any, K comparable](xs Stream[A], by func(A) K) map[K][]A

Group groups elements by a function that returns a key.

func GroupAggregate

func GroupAggregate[A, B any, K comparable](xs Stream[A], by func(A) K, aggregate func([]A) B) map[K]B

GroupAggregate is a convenience function that groups and then maps the subslices.

func Head[A any](xs Stream[A]) fun.Option[A]

Head takes the first element if present.

func Reduce

func Reduce[A, B any](start A, op func(A, B) A, xs Stream[B]) A

Reduce reduces stream into one value using given operation.

func Sum

func Sum[A fun.Number](xs Stream[A]) A

Sum finds sum of elements in stream.

func ToCounterBy

func ToCounterBy[A any, K comparable](xs Stream[A], by func(A) K) fun.Counter[K]

ToCounterBy consumes the stream and returns Counter with count of how many times each key was seen.

Types

type Chan

type Chan[T any] <-chan T

func (Chan[T]) For

func (c Chan[T]) For(f func(T) bool) error

type Dict

type Dict[K comparable, V any] map[K]V

func (Dict[K, V]) For

func (d Dict[K, V]) For(f func(K, V) bool) error

type Int

type Int int

func (Int) For

func (n Int) For(f func(int) bool) error

type PullFunc

type PullFunc[T any] func() (T, error)

func (PullFunc[T]) For

func (sf PullFunc[T]) For(f func(T) bool) error

type PushFunc

type PushFunc[T any] func(func(T) bool) bool

Must return true if yield has not returned false

func (PushFunc[T]) For

func (sf PushFunc[T]) For(f func(T) bool) error

type PushFunc2

type PushFunc2[K, V any] func(func(K, V) bool) bool

Must return true if yield has not returned false

func (PushFunc2[K, V]) For

func (sf PushFunc2[K, V]) For(f func(K, V) bool) error

type Slice

type Slice[T any] []T

func (Slice[T]) For

func (s Slice[T]) For(f func(int, T) bool) error

type Stream

type Stream[T any] interface {
	For(func(T) bool) error
}

Iterator2 is iterator taking function which handles iteratee and returns whether to stop iteration. Returns error happened during iteration or ErrBreak if exited prematurely.

func Chain

func Chain[A any](xss ...Stream[A]) Stream[A]

Chain appends another stream after the end of the first one.

func Chunked

func Chunked[A any](xs Stream[A], n int) Stream[[]A]

Chunked groups elements by n and produces a stream of slices. Produced chunks must not be retained.

func DebugPrint

func DebugPrint[A any](prefix string, xs Stream[A]) Stream[A]

DebugPrint prints every processed element, without changing it.

func Filter

func Filter[A any](xs Stream[A], p func(A) bool) Stream[A]

Filter leaves in the stream only the elements that satisfy the given predicate.

func FlatMap

func FlatMap[A, B any](xs Stream[A], f func(A) Stream[B]) Stream[B]

FlatMap maps stream using function and concatenates result streams into one.

func Flatten

func Flatten[A any](xs Stream[Stream[A]]) Stream[A]

Flatten simplifies a stream of streams to just the stream of values by concatenating all inner streams.

func FromMany

func FromMany[A any](as ...A) Stream[A]

FromMany returns a stream with all the given values.

func Generate

func Generate[A any](x0 A, f func(A) A) Stream[A]

Generate constructs an infinite stream of values using the production function.

func Intersperse

func Intersperse[A any](xs Stream[A], sep A) Stream[A]

Intersperse adds a separator after each stream element.

func Keys

func Keys[K, V any](xs Stream2[K, V]) Stream[K]

func Map

func Map[A, B any](xs Stream[A], f func(A) B) Stream[B]

Map converts values of the stream.

func MapFilter

func MapFilter[A, B any](xs Stream[A], f func(A) fun.Option[B]) Stream[B]

MapFilter applies function to every element and leaves only elements that are not None.

func NewGenerator

func NewGenerator[T any](f func(func(T))) Stream[T]

func NewStreamEmpty

func NewStreamEmpty[A any]() Stream[A]

NewStreamEmpty returns an empty stream.

func Once

func Once[A any](a A) Stream[A]

Once returns a stream of one value.

func Paged

func Paged[A any](xs Stream[[]A]) Stream[A]

Paged makes stream from stream of pages of elements represented as slices.

func Range

func Range[N constraints.Ordered](start, end, step N) Stream[N]

Range makes stream starting with start, step equal to step and going up to end, but not including end.

func Repeat

func Repeat[A any](xs Stream[A]) Stream[A]

Repeat appends the same stream infinitely.

func Skip

func Skip[A any](xs Stream[A], n int) Stream[A]

Skip skips n elements in the stream.

func Take

func Take[A any](xs Stream[A], n int) Stream[A]

Take cuts the stream after n elements.

func TakeWhile

func TakeWhile[A any](xs Stream[A], p func(A) bool) Stream[A]

TakeWhile takes elements while predicate is true.

func Unique

func Unique[A comparable](xs Stream[A]) Stream[A]

Unique makes stream of unique elements.

func Values

func Values[K, V any](xs Stream2[K, V]) Stream[V]

type Stream2

type Stream2[K, V any] interface {
	For(func(K, V) bool) error
}

type String

type String string

func (String) For

func (s String) For(f func(int, rune) bool) error

Jump to

Keyboard shortcuts

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