stream

package module
v0.0.0-...-c606a61 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2023 License: MIT Imports: 5 Imported by: 2

README

go-stream

Language GitHub go.mod Go version Release GitHub Workflow Status Go Report Card codecov

Overview

Go-Stream is a stream processing library to implement the Java Stream API with Go.

Features

  • non-storage: Stream is not a data structure, but the view of the data source. The data source can come from slice, map or supplier function.
  • pipeline: Operate the elements through pipeline, support short-circuit and parallel execution.
  • lazy-evaluation: The intermediate operation on the stream is lazy, and it will be truly executed only when the terminal operation is performed.

Go-Stream supports the following operations

Stream Operation
Intermediate operations Stateless Filter、Map、Peek、FlatMap
Stateful Distinct、Sorted、Skip、Limit、TakeWhile、DropWhile
Terminal operations non short-circuiting ForEach、Reduce、ReduceFromIdentity、Count、Max、Min、FindLast、ToSlice、ToMap、GroupingBy
short-circuiting AllMatch、AnyMatch、NoneMatch、FindFirst

Quick Start

  1. installation go-stream library
go get github.com/chinalhr/go-stream
  1. import
import "github.com/chinalhr/go-stream"
  1. Use Stream to process data
	type widget struct {
		color  string
		weight int
	}

	widgets := []widget{
		{
			color:  "yellow",
			weight: 4,
		},
		{
			color:  "red",
			weight: 3,
		},
		{
			color:  "yellow",
			weight: 2,
		},
		{
			color:  "blue",
			weight: 1,
		},
	}

	sum := stream.OfSlice(widgets).
		Filter(func(e types.T) bool {
			return e.(widget).color == "yellow"
		}).
		Map(func(e types.T) (r types.R) {
			return e.(widget).weight
		}).
		Reduce(func(e1 types.T, e2 types.T) types.T {
			return e1.(int) + e2.(int)
		})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Comparator

type Comparator struct {
	// contains filtered or unexported fields
}

func (*Comparator) Len

func (c *Comparator) Len() int

func (*Comparator) Less

func (c *Comparator) Less(i, j int) bool

func (*Comparator) Swap

func (c *Comparator) Swap(i, j int)

type Stream

type Stream struct {
	// contains filtered or unexported fields
}

Stream is a sequence of elements supporting sequential and parallel aggregate operations. Stream operations are combined into a stream pipeline, and computational processing is performed during terminal operation. Stream consists of a source(OfElements OfSlice OfMap Generate), zero or more intermediate operations(Filter Map Peek FlatMap Distinct Sorted Skip Limit TakeWhile DropWhile), and terminal operations(ForEach FindLast FindFirst Reduce ReduceFromIdentity Count Max Min ToSlice ToMap GroupingBy AllMatch AnyMatch NoneMatch FindFirst). Stream has lazy evaluation and short-circuit evaluation. Example See: _example/example.go

func Generate

func Generate(supplier func() types.T) Stream

Generate Return an infinite sequential Stream,elements are generated by the supplier.

func OfElements

func OfElements(elements ...types.T) Stream

OfElements Return a sequential Stream containing elements.

func OfMap

func OfMap(mapValue types.T) Stream

OfMap Return a sequential Stream containing mapValue.

func OfSlice

func OfSlice(slice types.T) Stream

OfSlice Return a sequential Stream containing slice.

func (Stream) AllMatch

func (s Stream) AllMatch(predicate func(e types.T) bool) bool

AllMatch Returns whether all elements of this Stream match the predicate function.

func (Stream) AnyMatch

func (s Stream) AnyMatch(predicate func(e types.T) bool) bool

AnyMatch Returns whether any elements of this Stream match the predicate function.

func (Stream) Count

func (s Stream) Count() int

Count Returns the count of elements in this Stream.

func (Stream) Distinct

func (s Stream) Distinct(distinctFn func(item types.T) types.R) Stream

Distinct Returns a Stream consisting of the distinct elements,confirm the uniqueness of the element through the distinctFn

func (Stream) DropWhile

func (s Stream) DropWhile(predicate func(t types.T) bool) Stream

DropWhile When the element matching function, start passing the element to Stream.

func (Stream) Filter

func (s Stream) Filter(predicate func(e types.T) bool) Stream

Filter Returns a Stream consisting of the elements of this stream that match the given predicate function.

func (Stream) FindFirst

func (s Stream) FindFirst() types.T

FindFirst Return the first element of the Stream.

func (Stream) FindLast

func (s Stream) FindLast() types.T

FindLast Return The last element of the Stream.

func (Stream) FlatMap

func (s Stream) FlatMap(mapper func(t types.T) Stream) Stream

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 mapper function to each element.

func (Stream) ForEach

func (s Stream) ForEach(action func(e types.T))

ForEach Performs an action for each element of this stream.

func (Stream) GroupingBy

func (s Stream) GroupingBy(classifier func(t types.T) types.K) map[types.K][]types.T

GroupingBy Returns a Map containing all elements of the Stream transformed by the classifier function.

func (Stream) Limit

func (s Stream) Limit(maxSize int) Stream

Limit Returns a stream consisting of the elements of this Stream, truncated to be no longer than maxSize in length.

func (Stream) Map

func (s Stream) Map(mapper func(e types.T) (r types.R)) Stream

Map Returns a Stream of elements transformed by the mapper function

func (Stream) Max

func (s Stream) Max(compare func(first types.T, second types.T) int) types.T

Max Compare through the compare function, return the max value in Stream.

func (Stream) Min

func (s Stream) Min(compare func(first types.T, second types.T) int) types.T

Min Compare through the compare function, return the min value in Stream.

func (Stream) NoneMatch

func (s Stream) NoneMatch(predicate func(e types.T) bool) bool

NoneMatch Returns whether any elements of this Stream non match the predicate function.

func (Stream) Parallel

func (s Stream) Parallel(workers int) Stream

Parallel Set the number of workers to perform Stream operations in parallel.

func (Stream) Peek

func (s Stream) Peek(consumer func(e types.T)) Stream

Peek Does not transform the Stream, executes the consumer function on the elements in the Stream.

func (Stream) Reduce

func (s Stream) Reduce(accumulator func(e1 types.T, e2 types.T) types.T) types.T

Reduce Performs a reduction on the elements of this Stream, using the associative accumulator function, and returns the reduced value.

func (Stream) ReduceFromIdentity

func (s Stream) ReduceFromIdentity(identity types.T, accumulator func(e1 types.T, e2 types.T) types.T) types.T

ReduceFromIdentity Performs a reduction on the elements of this Stream, using the provided identity value and an associative accumulator function, and returns the reduced value.

func (Stream) Skip

func (s Stream) Skip(n int) Stream

Skip Discard the previous n elements, return the Stream of the remaining elements.

func (Stream) Sorted

func (s Stream) Sorted(compare func(first types.T, second types.T) int) Stream

Sorted Return to orderly Stream. sort elements in Stream based on compare function.

func (Stream) TakeWhile

func (s Stream) TakeWhile(predicate func(t types.T) bool) Stream

TakeWhile Truncate Stream when function does not match.

func (Stream) ToMap

func (s Stream) ToMap(keyMapper func(t types.T) types.K, valueMapper func(t types.T) types.R) map[types.K]types.R

ToMap Returns a Map containing all elements of the Stream transformed by the keyMapper function.

func (Stream) ToSlice

func (s Stream) ToSlice() []types.T

ToSlice Returns a Slice Containing all elements of the Stream.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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