stream

package
v0.0.0-...-42d93e8 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAvgCollector

func NewAvgCollector[T any, R constraints.Integer | constraints.Float](
	mapper func(T) R) (
	supplier func() any,
	accumulator func(container, item any),
	finisher func(container any) any,
)

func NewCountCollector

func NewCountCollector[T any]() (
	supplier func() any,
	accumulator func(container, item any),
	finisher func(container any) any,
)

func NewGroupByCollector

func NewGroupByCollector[T any, K comparable](
	keyMapper func(T) K,
) (
	supplier func() any,
	accumulator func(container, item any),
	finisher func(container any) any,
)

func NewJoiningCollector

func NewJoiningCollector[T any](separator string) (
	supplier func() any,
	accumulator func(container, item any),
	finisher func(container any) any,
)

func NewMaxCollector

func NewMaxCollector[T any, R constraints.Ordered](
	mapper func(T) R) (
	supplier func() any,
	accumulator func(container, item any),
	finisher func(container any) any,
)

func NewMinCollector

func NewMinCollector[T any, R constraints.Ordered](
	mapper func(T) R) (
	supplier func() any,
	accumulator func(container, item any),
	finisher func(container any) any,
)

func NewSumCollector

func NewSumCollector[T any, R constraints.Integer | constraints.Float](
	mapper func(T) R) (
	supplier func() any,
	accumulator func(container, item any),
	finisher func(container any) any,
)

func NewToMapCollector

func NewToMapCollector[T any, K comparable](
	size int,
	keyMapper func(T) K,
) (
	supplier func() any,
	accumulator func(container, item any),
	finisher func(container any) any,
)

NewToMapCollector returns a collector that accumulates the input elements into a map whose keys and values are the result of applying the provided mapping functions to the input elements.

size is the expected size of the map.

keyMapper is a function that maps the input element to a key.

Note: If the input elements contain duplicate keys, will panic.

Note: This collector is not routine-safe.

func NewToMapCollectorWithDuplicateHandler

func NewToMapCollectorWithDuplicateHandler[T any, K comparable, V any](
	size int,
	keyMapper func(T) K,
	valueMapper func(T) V,
	duplicateHandler func(duplicateKey K, existingValue V, newValue V) V,
) (
	supplier func() any,
	accumulator func(container, item any),
	finisher func(container any) any,
)

NewToMapCollectorWithDuplicateHandler returns a collector that accumulates the input elements into a map whose keys and values are the result of applying the provided mapping functions to the input elements.

size is the expected size of the map.

keyMapper is a function that maps the input element to a key.

valueMapper is a function that maps the input element to a value.

duplicateHandler is a function that handles the duplicate key. If it is nil, will ignore the duplicate key.

Note: This collector is not routine-safe.

func NewToMapWithIgnoreDuplicateCollector

func NewToMapWithIgnoreDuplicateCollector[T any, K comparable](
	size int,
	keyMapper func(T) K,
) (
	supplier func() any,
	accumulator func(container, item any),
	finisher func(container any) any,
)

NewToMapWithIgnoreDuplicateCollector returns a collector that accumulates the input elements into a map whose keys and values are the result of applying the provided mapping functions to the input elements.

size is the expected size of the map.

keyMapper is a function that maps the input element to a key.

Note: If the input elements contain duplicate keys, the last one will be kept.

Note: This collector is not routine-safe.

func NewToSliceCollector

func NewToSliceCollector[T any](
	size int,
) (
	supplier func() any,
	accumulator func(container, item any),
	finisher func(container any) any,
)

func WithParallelism

func WithParallelism(parallelism uint) func(Stream)

WithParallelism returns an option that sets the parallelism of the stream

func WithSync

func WithSync() func(Stream)

WithSync returns an option that sets the sync of the stream

Types

type AccumulatorFunc

type AccumulatorFunc func(identity any, item any) any

type ConsumeFunc

type ConsumeFunc func(item any)

type DistinctFunc

type DistinctFunc func(item any) any

type FilterFunc

type FilterFunc func(item any) bool

type FlatMapFunc

type FlatMapFunc func(item any) Stream

type GenerateFunc

type GenerateFunc func(source chan<- any)

source is the channel that the generator function writes to, and the generator should not close the channel

type LessFunc

type LessFunc func(a, b any) bool

type MapFunc

type MapFunc func(item any) any

type MatchFunc

type MatchFunc FilterFunc

type Option

type Option func(s Stream)

type Stream

type Stream interface {
	// Map applies the given mapper to each item in the stream
	Map(mapper MapFunc, opts ...Option) Stream
	// FlatMap applies the given mapper to each item in the stream
	FlatMap(mapper FlatMapFunc, opts ...Option) Stream
	// Filter filters the stream by the given predicate
	Filter(filter FilterFunc, opts ...Option) Stream
	// Concat concatenates the given streams to the current stream
	Concat(streams []Stream, opts ...Option) Stream
	// Sort sorts the stream by the given less function
	Sort(less LessFunc, opts ...Option) Stream
	// Distinct removes the duplicate items in the stream
	Distinct(distinct DistinctFunc, opts ...Option) Stream
	// Skip skips the first n items in the stream
	Skip(limit int64, opts ...Option) Stream
	// Limit limits the number of items in the stream
	Limit(limit int64, opts ...Option) Stream
	// TakeWhile takes items from the stream while the given predicate is true.
	// The first item that makes the predicate false will stop the stream.
	//
	// This is a short-circuiting terminal operation.
	TakeWhile(match MatchFunc, opts ...Option) Stream
	// DropWhile drops items from the stream while the given predicate is true.
	// The first item that makes the predicate false will start the stream.
	DropWhile(match MatchFunc, opts ...Option) Stream
	// Peek applies the given consumer to each item in the stream
	Peek(consumer ConsumeFunc, opts ...Option) Stream

	// AnyMatch returns true if any item in the stream matches the given predicate, otherwise false.
	// If the stream is empty, false is returned.
	//
	// This is a short-circuiting terminal operation.
	AnyMatch(match MatchFunc, opts ...Option) bool
	// AllMatch returns true if all items in the stream match the given predicate, otherwise false.
	// If the stream is empty, true is returned.
	//
	// This is a short-circuiting terminal operation.
	AllMatch(match MatchFunc, opts ...Option) bool
	// NoneMatch returns true if no item in the stream matches the given predicate, otherwise false.
	// If the stream is empty, true is returned.
	//
	// This is a short-circuiting terminal operation.
	NoneMatch(match MatchFunc, opts ...Option) bool
	// FindFirst returns the first item in the stream.
	// If the stream is empty, nil is returned.
	//
	// If the stream has no encounter order, then any element may be returned.
	//
	// This is a short-circuiting terminal operation.
	FindFirst(opts ...Option) (item any, found bool)
	// Count returns the number of items in the stream.
	// If the count is greater than math.MaxInt64, math.MaxInt64 is returned.
	Count(opts ...Option) int64
	// Reduce reduces the stream to a single value by the given accumulator function.
	Reduce(identity any, accumulator AccumulatorFunc, opts ...Option) any
	// ForEach applies the given consumer to each item in the stream
	ForEach(consumer ConsumeFunc, opts ...Option)
	// ToIfaceSlice returns the stream as a slice of interface{}
	ToIfaceSlice(opts ...Option) []any
	// ApplyOptions applies the given options to the stream
	ApplyOptions(opts ...Option) Stream
	// Collect collects the stream to a supplier of the given type.
	//
	// You usually don't need to set these 3 parameters directly unless you want to customize the collector.
	// Here are some common collectors:
	// - NewToMapCollector
	// - NewToMapWithIgnoreDuplicateCollector
	// - NewToMapCollectorWithDuplicateHandler
	// - NewToSliceCollector
	// - NewJoiningCollector
	// - NewGroupByCollector
	// - NewCountCollector
	// - NewSumCollector
	// - NewAvgCollector
	// - NewMaxCollector
	// - NewMinCollector
	//
	// Please refer to the example in collectors_test.go for more details.
	Collect(
		supplier func() any,
		accumulator func(container, item any),
		finisher func(container any) any,
	) any
	// Close closes the stream
	Close()
}

Stream is the interface for a stream

func Concat

func Concat(first Stream, other []Stream, opts ...Option) Stream

Concat concatenates the given streams to a single stream

func From

func From(generator GenerateFunc, opts ...Option) Stream

From returns a stream from the given generator function

func Just

func Just[T any](items []T, opts ...Option) Stream

Just returns a stream of the given items

func Range

func Range[T constraints.Integer](startInclude, endExclusive T, opts ...Option) Stream

Range returns a stream of integer from start (inclusive) to end (exclusive)

startInclude indicates whether start is included in the stream

endExclusive indicates whether end is excluded in the stream

type SupplierFunc

type SupplierFunc func() any

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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