stream

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2022 License: Apache-2.0 Imports: 3 Imported by: 7

Documentation

Overview

Package stream provides type-safe streams, functional helper tools and processing operations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllMatch

func AllMatch[T any](input Stream[T], predicate func(T) bool) bool

AllMatch returns whether all elements of this stream match the provided predicate. If this operation finds an item where the predicate is false, it stops processing the rest of the stream. This function is equivalent to invoking input.AllMatch(predicate) as method

func AnyMatch

func AnyMatch[T any](input Stream[T], predicate func(T) bool) bool

AnyMatch returns whether any elements of this stream match the provided predicate. If this operation finds an item where the predicate is true, it stops processing the rest of the stream. This function is equivalent to invoking input.AnyMatch(predicate) as method

func Count

func Count[T any](input Stream[T]) int

Count of elements in this stream. This function is equivalent to invoking input.Count() as method.

func FindFirst

func FindFirst[T any](input Stream[T]) (T, bool)

FindFirst returns the first element of this Stream along with true or, if the stream is empty, the zero value of the inner type along with false. This function is equivalent to invoking input.FindFirst() as method.

func ForEach

func ForEach[T any](input Stream[T], consumer func(T))

ForEach invokes the consumer function for each item of the Stream. This function is equivalent to invoking input.ForEach(consumer) as method.

func Max

func Max[T any](input Stream[T], cmp order.Comparator[T]) (T, bool)

Max returns the maximum element of this stream according to the provided Comparator, along with true if the stream is not empty. If the stream is empty, returns the zero value along with false. This function is equivalent to invoking input.Max(cmp) as method.

func Min

func Min[T any](input Stream[T], cmp order.Comparator[T]) (T, bool)

Min returns the minimum element of this stream according to the provided Comparator, along with true if the stream is not empty. If the stream is empty, returns the zero value along with false. This function is equivalent to invoking input.Min(cmp) as method.

func NoneMatch

func NoneMatch[T any](input Stream[T], predicate func(T) bool) bool

NoneMatch returns whether no elements of this stream match the provided predicate. If this operation finds an item where the predicate is true, it stops processing the rest of the stream. This function is equivalent to invoking input.NoneMatch(predicate) as method.

func Reduce

func Reduce[T any](input Stream[T], accumulator func(a, b T) T) (T, bool)

Reduce performs a reduction on the elements of this stream, using an associative accumulation function, and returns an value describing the reduced value, if any. If no reduced value (e.g. because the stream is empty), the second returned value is false. This function is equivalent to invoking input.Reduce(accumulator) as method.

func ToMap

func ToMap[K comparable, V any](input Stream[item.Pair[K, V]]) map[K]V

ToMap returns a map Containing all the item.Pair elements of this Stream, where the Key/Val fields of the item.Pair represents the key/value of the map, respectively.

func ToSlice

func ToSlice[T any](input Stream[T]) []T

ToSlice returns a Slice Containing all the elements of this Stream. This function is equivalent to invoking input.ToSlice() as method.

Types

type Stream

type Stream[T any] interface {

	// Filter returns a Stream consisting of the items of this stream that match the given
	// predicate (this is, applying the predicate function over the item returns true).
	Filter(predicate func(T) bool) Stream[T]

	// FlatMap returns a stream consisting of the results of replacing each element of this stream
	// with the contents of a mapped stream produced by applying the provided mapping function to
	// each element. Each mapped stream is closed after its contents have been placed into this
	// stream. (If a mapped stream is null an empty stream is used, instead.)
	//
	// Due to the lazy nature of streams, if any of the mapped streams is infinite it will remain
	// unnoticed and some operations (Count, Reduce, Sorted, AllMatch...) will not end.
	//
	// If you need that the input and output Stream contain elements from different types, you need
	// to invoke the standalone function FlatMap[IN,OUT](Stream[IN], func(IN)OUT) Stream[OUT].
	FlatMap(mapper func(T) Stream[T]) Stream[T]

	// Limit returns a stream consisting of the elements of this stream, truncated to
	// be no longer than maxSize in length.
	Limit(maxSize int) Stream[T]

	// Map returns a Stream consisting of the results of individually applying
	// the mapper function to each element of this Stream. The argument and return
	// value of the mapper function must belong to the same type. If you need that
	// the input and output Stream contain elements from different types, you need
	// to invoke the standalone function Map[IN,OUT](Stream[IN], func(IN)OUT) Stream[OUT].
	Map(mapper func(T) T) Stream[T]

	// Peek peturns a stream consisting of the elements of this stream, additionally performing
	// the provided action on each element as elements are consumed from the resulting stream.
	Peek(consumer func(T)) Stream[T]

	// Skip returns a stream consisting of the remaining elements of this stream after discarding
	// the first n elements of the stream.
	Skip(n int) Stream[T]

	// Sorted returns a stream consisting of the elements of this stream, sorted according
	// to the provided order.Comparator.
	Sorted(comparator order.Comparator[T]) Stream[T]

	// AllMatch returns whether all elements of this stream match the provided predicate.
	// If this operation finds an item where the predicate is false, it stops processing
	// the rest of the stream.
	AllMatch(predicate func(T) bool) bool

	// AnyMatch returns whether any elements of this stream match the provided predicate.
	// If this operation finds an item where the predicate is true, it stops processing
	// the rest of the stream.
	AnyMatch(predicate func(T) bool) bool

	// Count of elements in this stream.
	Count() int

	// FindFirst returns the first element of this Stream along with true or, if the
	// stream is empty, the zero value of the inner type along with false.
	FindFirst() (T, bool)

	// ForEach invokes the consumer function for each item of the Stream.
	ForEach(consumer func(T))

	// Max returns the maximum element of this stream according to the provided Comparator,
	// along with true if the stream is not empty. If the stream is empty, returns the zero
	// value along with false.
	Max(cmp order.Comparator[T]) (T, bool)

	// Min returns the minimum element of this stream according to the provided Comparator,
	// along with true if the stream is not empty. If the stream is empty, returns the zero
	// value along with false.
	Min(cmp order.Comparator[T]) (T, bool)

	// NoneMatch returns whether no elements of this stream match the provided predicate.
	// If this operation finds an item where the predicate is true, it stops processing
	// the rest of the stream.
	NoneMatch(predicate func(T) bool) bool

	// Reduce performs a reduction on the elements of this stream, using an associative
	// accumulation function, and returns an value describing the reduced value, if any.
	// If no reduced value (e.g. because the stream is empty), the second returned value
	// is false.
	Reduce(accumulator func(a, b T) T) (T, bool)

	// ToSlice returns a Slice Containing all the elements of this Stream.
	ToSlice() []T
	// contains filtered or unexported methods
}

Stream is a sequence of elements supporting different processing and aggregation functionalities. To perform a computation, Stream operations are composed into a stream pipeline. A stream pipeline consists of a source (which might be an array, a collection, a generator function, an I/O channel, etc), zero or more intermediate operations (which transform a stream into another stream, such as filter(predicate)), and a terminal operation (which produces a result or side-effect, such as reduce(function) or forEach(consumer)). Streams are lazy; computation on the source data is only performed when the terminal operation is initiated, and source elements are consumed only as needed.

func Concat

func Concat[T any](a, b Stream[T]) Stream[T]

Concat creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.

func Distinct

func Distinct[T comparable](input Stream[T]) Stream[T]

Distinct returns a stream consisting of the distinct elements (according to equality operator) of the input stream.

func Empty

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

Empty returns an empty stream

func Filter

func Filter[T any](input Stream[T], predicate func(T) bool) Stream[T]

Filter returns a Stream consisting of the items of this stream that match the given predicate (this is, applying the predicate function over the item returns true). This function is equivalent to invoking input.Filter(predicate) as method.

func FlatMap

func FlatMap[IN, OUT any](input Stream[IN], mapper func(IN) Stream[OUT]) Stream[OUT]

FlatMap returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Each mapped stream is closed after its contents have been placed into this stream. (If a mapped stream is null an empty stream is used, instead.)

Due to the lazy nature of streams, if any of the mapped streams is infinite it will remain unnoticed and some operations (Count, Reduce, Sorted, AllMatch...) will not end.

When both the input and output type are the same, the operation can be invoked as the method input.FlatMap(mapper).

func Generate

func Generate[T any](supplier func() T) Stream[T]

Generate an infinite sequential stream where each element is generated by the provided supplier function. Due to the stateful nature of the supplier, multiple operations towards the same stream might provide different results.

func Iterate

func Iterate[T any](seed T, f func(T) T) Stream[T]

Iterate returns an infinite sequential ordered Stream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc. The first element (position 0) in the Stream will be the provided seed. For n > 0, the element at position n, will be the result of applying the function f to the element at position n - 1. Due to the stateful nature of the supplier, multiple operations towards the same stream might provide different results.

func Limit

func Limit[T any](input Stream[T], maxSize int) Stream[T]

Limit returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length. This function is equivalent to invoking input.Limit(maxSize) as method.

func Map

func Map[IT, OT any](input Stream[IT], mapper func(IT) OT) Stream[OT]

Map returns a Stream consisting of the results of individually applying the mapper function to each elements of the input Stream. When both the input and output type are the same, the operation can be invoked as the method input.Map(mapper).

func Of

func Of[T any](elems ...T) Stream[T]

Of creates an Stream from a variable number of elements that are passed as arguments.

func OfChannel

func OfChannel[T any](source <-chan T) Stream[T]

OfChannel creates a Stream from an input channel. The Stream won't end until the source channel is closed, so some operations (Distinct, Sorted, ToSlice, Count...) will block the execution until the source is closed.

func OfMap

func OfMap[K comparable, V any](source map[K]V) Stream[item.Pair[K, V]]

OfMap creates a Stream of item.Pair elements. Each kv.Pair corresponds to a key/value entry of the source map.

func OfSlice

func OfSlice[T any](elems []T) Stream[T]

OfSlice creates a Stream from a slice.

func Peek

func Peek[T any](input Stream[T], consumer func(T)) Stream[T]

Peek peturns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream. This function is equivalent to invoking input.Peek(consumer) as method.

func Skip

func Skip[T any](input Stream[T], n int) Stream[T]

Skip returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. This function is equivalent to invoking input.Skip(n) as method.

func Sorted

func Sorted[T any](input Stream[T], comparator order.Comparator[T]) Stream[T]

Sorted returns a stream consisting of the elements of this stream, sorted according to the provided order.Comparator. This function is equivalent to invoking input.Sorted(comparator) as method.

Jump to

Keyboard shortcuts

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