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 ¶
- type Stream
- func Concat[T any](a, b Stream[T]) Stream[T]
- func FromChannel[T any](source <-chan T) Stream[T]
- func FromRange[T constraints.Integer | constraints.Float](start, end, step T) Stream[T]
- func FromSlice[T any](source []T) Stream[T]
- func Generate[T any](generator func() func() (item T, ok bool)) Stream[T]
- func Of[T any](elems ...T) Stream[T]
- func (s Stream[T]) AllMatch(predicate func(item T) bool) bool
- func (s Stream[T]) AnyMatch(predicate func(item T) bool) bool
- func (s Stream[T]) Count() int
- func (s Stream[T]) Distinct() Stream[T]
- func (s Stream[T]) Filter(predicate func(item T) bool) Stream[T]
- func (s Stream[T]) FindFirst() (T, bool)
- func (s Stream[T]) FindLast() (T, bool)
- func (s Stream[T]) ForEach(action func(item T))
- func (s Stream[T]) Limit(maxSize int) Stream[T]
- func (s Stream[T]) Map(mapper func(item T) T) Stream[T]
- func (s Stream[T]) Max(less func(a, b T) bool) (T, bool)
- func (s Stream[T]) Min(less func(a, b T) bool) (T, bool)
- func (s Stream[T]) NoneMatch(predicate func(item T) bool) bool
- func (s Stream[T]) Peek(consumer func(item T)) Stream[T]
- func (s Stream[T]) Range(start, end int) Stream[T]
- func (s Stream[T]) Reduce(initial T, accumulator func(a, b T) T) T
- func (s Stream[T]) Reverse() Stream[T]
- func (s Stream[T]) Skip(n int) Stream[T]
- func (s Stream[T]) Sorted(less func(a, b T) bool) Stream[T]
- func (s Stream[T]) ToSlice() []T
Examples ¶
- Concat
- FromChannel
- FromRange
- FromSlice
- Generate
- Of
- Stream.AllMatch
- Stream.AnyMatch
- Stream.Count
- Stream.Distinct
- Stream.Filter
- Stream.FindFirst
- Stream.FindLast
- Stream.ForEach
- Stream.Limit
- Stream.Map
- Stream.Max
- Stream.Min
- Stream.NoneMatch
- Stream.Peek
- Stream.Range
- Stream.Reduce
- Stream.Reverse
- Stream.Skip
- Stream.Sorted
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Stream ¶
type Stream[T any] struct { // contains filtered or unexported fields }
func Concat ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
func (s Stream[T]) ToSlice() []T
ToSlice return the elements in the stream. Play: https://go.dev/play/p/jI6_iZZuVFE