stream

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package stream implements a sequence of elements supporting sequential and operations. this package is an experiment to explore if stream in go can work as the way java does. its function is very limited.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stream added in v2.3.0

type Stream[T any] struct {
	// contains filtered or unexported fields
}

func Concat added in v2.1.15

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. Play: https://go.dev/play/p/HM4OlYk_OUC

Example
s1 := FromSlice([]int{1, 2, 3})
s2 := FromSlice([]int{4, 5, 6})

s := Concat(s1, s2)

data := s.ToSlice()

fmt.Println(data)
Output:

[1 2 3 4 5 6]

func FromChannel

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

FromChannel creates stream from channel. Play: https://go.dev/play/p/9TZYugGMhXZ

Example
ch := make(chan int)
go func() {
	for i := 1; i < 4; i++ {
		ch <- i
	}
	close(ch)
}()

s := FromChannel(ch)

data := s.ToSlice()

fmt.Println(data)
Output:

[1 2 3]

func FromRange

func FromRange[T constraints.Integer | constraints.Float](start, end, step T) Stream[T]

FromRange creates a number stream from start to end. both start and end are included. [start, end] Play: https://go.dev/play/p/9Ex1-zcg-B-

Example
s := FromRange(1, 5, 1)

data := s.ToSlice()
fmt.Println(data)
Output:

[1 2 3 4 5]

func FromSlice

func FromSlice[T any](source []T) Stream[T]

FromSlice creates stream from slice. Play: https://go.dev/play/p/wywTO0XZtI4

Example
s := FromSlice([]int{1, 2, 3})

data := s.ToSlice()

fmt.Println(data)
Output:

[1 2 3]

func Generate

func Generate[T any](generator func() func() (item T, ok bool)) Stream[T]

Generate stream where each element is generated by the provided generater function Play: https://go.dev/play/p/rkOWL1yA3j9

Example
n := 0
max := 4

generator := func() func() (int, bool) {
	return func() (int, bool) {
		n++
		return n, n < max
	}
}

s := Generate(generator)

data := s.ToSlice()

fmt.Println(data)
Output:

[1 2 3]

func Of

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

Of creates a stream whose elements are the specified values. Play: https://go.dev/play/p/jI6_iZZuVFE

Example
s := Of(1, 2, 3)

data := s.ToSlice()

fmt.Println(data)
Output:

[1 2 3]

func (Stream[T]) AllMatch added in v2.3.0

func (s Stream[T]) AllMatch(predicate func(item T) bool) bool

AllMatch returns whether all elements of this stream match the provided predicate. Play: https://go.dev/play/p/V5TBpVRs-Cx

Example
original := FromSlice([]int{1, 2, 3})

result1 := original.AllMatch(func(item int) bool {
	return item > 0
})

result2 := original.AllMatch(func(item int) bool {
	return item > 1
})

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func (Stream[T]) AnyMatch added in v2.3.0

func (s Stream[T]) AnyMatch(predicate func(item T) bool) bool

AnyMatch returns whether any elements of this stream match the provided predicate. Play: https://go.dev/play/p/PTCnWn4OxSn

Example
original := FromSlice([]int{1, 2, 3})

result1 := original.AnyMatch(func(item int) bool {
	return item > 1
})

result2 := original.AnyMatch(func(item int) bool {
	return item > 3
})

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func (Stream[T]) Count added in v2.3.0

func (s Stream[T]) Count() int

Count returns the count of elements in the stream. Play: https://go.dev/play/p/r3koY6y_Xo-

Example
s1 := FromSlice([]int{1, 2, 3})
s2 := FromSlice([]int{})

fmt.Println(s1.Count())
fmt.Println(s2.Count())
Output:

3
0

func (Stream[T]) Distinct added in v2.3.0

func (s Stream[T]) Distinct() Stream[T]

Distinct returns a stream that removes the duplicated items. Play: https://go.dev/play/p/eGkOSrm64cB

Example
original := FromSlice([]int{1, 2, 2, 3, 3, 3})
distinct := original.Distinct()

data1 := original.ToSlice()
data2 := distinct.ToSlice()

fmt.Println(data1)
fmt.Println(data2)
Output:

[1 2 2 3 3 3]
[1 2 3]

func (Stream[T]) Filter added in v2.3.0

func (s Stream[T]) Filter(predicate func(item T) bool) Stream[T]

Filter returns a stream consisting of the elements of this stream that match the given predicate. Play: https://go.dev/play/p/MFlSANo-buc

Example
original := FromSlice([]int{1, 2, 3, 4, 5})

isEven := func(n int) bool {
	return n%2 == 0
}

even := original.Filter(isEven)

fmt.Println(even.ToSlice())
Output:

[2 4]

func (Stream[T]) FindFirst added in v2.3.0

func (s Stream[T]) FindFirst() (T, bool)

FindFirst returns the first element of this stream and true, or zero value and false if the stream is empty. Play: https://go.dev/play/p/9xEf0-6C1e3

Example
original := FromSlice([]int{1, 2, 3})

result, ok := original.FindFirst()

fmt.Println(result)
fmt.Println(ok)
Output:

1
true

func (Stream[T]) FindLast added in v2.3.0

func (s Stream[T]) FindLast() (T, bool)

FindLast returns the last element of this stream and true, or zero value and false if the stream is empty. Play: https://go.dev/play/p/WZD2rDAW-2h

Example
original := FromSlice([]int{3, 2, 1})

result, ok := original.FindLast()

fmt.Println(result)
fmt.Println(ok)
Output:

1
true

func (Stream[T]) ForEach added in v2.3.0

func (s Stream[T]) ForEach(action func(item T))

ForEach performs an action for each element of this stream. Play: https://go.dev/play/p/Dsm0fPqcidk

Example
original := FromSlice([]int{1, 2, 3})

result := 0
original.ForEach(func(item int) {
	result += item
})

fmt.Println(result)
Output:

6

func (Stream[T]) Limit added in v2.3.0

func (s Stream[T]) Limit(maxSize int) Stream[T]

Limit returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length. Play: https://go.dev/play/p/qsO4aniDcGf

Example
original := FromSlice([]int{1, 2, 3, 4})

s1 := original.Limit(-1)
s2 := original.Limit(0)
s3 := original.Limit(1)
s4 := original.Limit(5)

fmt.Println(s1.ToSlice())
fmt.Println(s2.ToSlice())
fmt.Println(s3.ToSlice())
fmt.Println(s4.ToSlice())
Output:

[]
[]
[1]
[1 2 3 4]

func (Stream[T]) Map added in v2.3.0

func (s Stream[T]) Map(mapper func(item T) T) Stream[T]

Map returns a stream consisting of the elements of this stream that apply the given function to elements of stream. Play: https://go.dev/play/p/OtNQUImdYko

Example
original := FromSlice([]int{1, 2, 3})

addOne := func(n int) int {
	return n + 1
}

increament := original.Map(addOne)

fmt.Println(increament.ToSlice())
Output:

[2 3 4]

func (Stream[T]) Max added in v2.3.0

func (s Stream[T]) Max(less func(a, b T) bool) (T, bool)

Max returns the maximum element of this stream according to the provided less function. less: a > b Play: https://go.dev/play/p/fm-1KOPtGzn

Example
original := FromSlice([]int{4, 2, 1, 3})

max, ok := original.Max(func(a, b int) bool { return a > b })

fmt.Println(max)
fmt.Println(ok)
Output:

4
true

func (Stream[T]) Min added in v2.3.0

func (s Stream[T]) Min(less func(a, b T) bool) (T, bool)

Min returns the minimum element of this stream according to the provided less function. less: a < b Play: https://go.dev/play/p/vZfIDgGNRe_0

Example
original := FromSlice([]int{4, 2, 1, 3})

min, ok := original.Min(func(a, b int) bool { return a < b })

fmt.Println(min)
fmt.Println(ok)
Output:

1
true

func (Stream[T]) NoneMatch added in v2.3.0

func (s Stream[T]) NoneMatch(predicate func(item T) bool) bool

NoneMatch returns whether no elements of this stream match the provided predicate. Play: https://go.dev/play/p/iWS64pL1oo3

Example
original := FromSlice([]int{1, 2, 3})

result1 := original.NoneMatch(func(item int) bool {
	return item > 3
})

result2 := original.NoneMatch(func(item int) bool {
	return item > 1
})

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func (Stream[T]) Peek added in v2.3.0

func (s Stream[T]) Peek(consumer func(item T)) Stream[T]

Peek returns 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. Play: https://go.dev/play/p/u1VNzHs6cb2

Example
original := FromSlice([]int{1, 2, 3})

data := []string{}
peekStream := original.Peek(func(n int) {
	data = append(data, fmt.Sprint("value", n))
})

fmt.Println(original.ToSlice())
fmt.Println(peekStream.ToSlice())
fmt.Println(data)
Output:

[1 2 3]
[1 2 3]
[value1 value2 value3]

func (Stream[T]) Range added in v2.3.0

func (s Stream[T]) Range(start, end int) Stream[T]

Range returns a stream whose elements are in the range from start(included) to end(excluded) original stream. Play: https://go.dev/play/p/indZY5V2f4j

Example
original := FromSlice([]int{1, 2, 3})

s1 := original.Range(0, 0)
s2 := original.Range(0, 1)
s3 := original.Range(0, 3)
s4 := original.Range(1, 2)

fmt.Println(s1.ToSlice())
fmt.Println(s2.ToSlice())
fmt.Println(s3.ToSlice())
fmt.Println(s4.ToSlice())
Output:

[]
[1]
[1 2 3]
[2]

func (Stream[T]) Reduce added in v2.3.0

func (s Stream[T]) Reduce(initial T, accumulator func(a, b T) T) T

Reduce performs a reduction on the elements of this stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any. Play: https://go.dev/play/p/6uzZjq_DJLU

Example
original := FromSlice([]int{1, 2, 3})

result := original.Reduce(0, func(a, b int) int {
	return a + b
})

fmt.Println(result)
Output:

6

func (Stream[T]) Reverse added in v2.3.0

func (s Stream[T]) Reverse() Stream[T]

Reverse returns a stream whose elements are reverse order of given stream. Play: https://go.dev/play/p/A8_zkJnLHm4

Example
original := FromSlice([]int{1, 2, 3})

reverse := original.Reverse()

fmt.Println(reverse.ToSlice())
Output:

[3 2 1]

func (Stream[T]) Skip added in v2.3.0

func (s Stream[T]) Skip(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. If this stream contains fewer than n elements then an empty stream will be returned. Play: https://go.dev/play/p/fNdHbqjahum

Example
original := FromSlice([]int{1, 2, 3, 4})

s1 := original.Skip(-1)
s2 := original.Skip(0)
s3 := original.Skip(1)
s4 := original.Skip(5)

fmt.Println(s1.ToSlice())
fmt.Println(s2.ToSlice())
fmt.Println(s3.ToSlice())
fmt.Println(s4.ToSlice())
Output:

[1 2 3 4]
[1 2 3 4]
[2 3 4]
[]

func (Stream[T]) Sorted added in v2.3.0

func (s Stream[T]) Sorted(less func(a, b T) bool) Stream[T]

Sorted returns a stream consisting of the elements of this stream, sorted according to the provided less function. Play: https://go.dev/play/p/XXtng5uonFj

Example
original := FromSlice([]int{4, 2, 1, 3})

sorted := original.Sorted(func(a, b int) bool { return a < b })

fmt.Println(original.ToSlice())
fmt.Println(sorted.ToSlice())
Output:

[4 2 1 3]
[1 2 3 4]

func (Stream[T]) ToSlice added in v2.3.0

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

ToSlice return the elements in the stream. Play: https://go.dev/play/p/jI6_iZZuVFE

Jump to

Keyboard shortcuts

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