mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-17 11:12:28 +08:00
refactor: make stream struct exported
This commit is contained in:
@@ -47,19 +47,19 @@ import (
|
|||||||
// Concat(streams ...StreamI[T]) StreamI[T]
|
// Concat(streams ...StreamI[T]) StreamI[T]
|
||||||
// }
|
// }
|
||||||
|
|
||||||
type stream[T any] struct {
|
type Stream[T any] struct {
|
||||||
source []T
|
source []T
|
||||||
}
|
}
|
||||||
|
|
||||||
// Of creates a stream whose elements are the specified values.
|
// Of creates a stream whose elements are the specified values.
|
||||||
// Play: https://go.dev/play/p/jI6_iZZuVFE
|
// Play: https://go.dev/play/p/jI6_iZZuVFE
|
||||||
func Of[T any](elems ...T) stream[T] {
|
func Of[T any](elems ...T) Stream[T] {
|
||||||
return FromSlice(elems)
|
return FromSlice(elems)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate stream where each element is generated by the provided generater function
|
// Generate stream where each element is generated by the provided generater function
|
||||||
// Play: https://go.dev/play/p/rkOWL1yA3j9
|
// Play: https://go.dev/play/p/rkOWL1yA3j9
|
||||||
func Generate[T any](generator func() func() (item T, ok bool)) stream[T] {
|
func Generate[T any](generator func() func() (item T, ok bool)) Stream[T] {
|
||||||
source := make([]T, 0)
|
source := make([]T, 0)
|
||||||
|
|
||||||
var zeroValue T
|
var zeroValue T
|
||||||
@@ -76,13 +76,13 @@ func Generate[T any](generator func() func() (item T, ok bool)) stream[T] {
|
|||||||
|
|
||||||
// FromSlice creates stream from slice.
|
// FromSlice creates stream from slice.
|
||||||
// Play: https://go.dev/play/p/wywTO0XZtI4
|
// Play: https://go.dev/play/p/wywTO0XZtI4
|
||||||
func FromSlice[T any](source []T) stream[T] {
|
func FromSlice[T any](source []T) Stream[T] {
|
||||||
return stream[T]{source: source}
|
return Stream[T]{source: source}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromChannel creates stream from channel.
|
// FromChannel creates stream from channel.
|
||||||
// Play: https://go.dev/play/p/9TZYugGMhXZ
|
// Play: https://go.dev/play/p/9TZYugGMhXZ
|
||||||
func FromChannel[T any](source <-chan T) stream[T] {
|
func FromChannel[T any](source <-chan T) Stream[T] {
|
||||||
s := make([]T, 0)
|
s := make([]T, 0)
|
||||||
|
|
||||||
for v := range source {
|
for v := range source {
|
||||||
@@ -94,7 +94,7 @@ func FromChannel[T any](source <-chan T) stream[T] {
|
|||||||
|
|
||||||
// FromRange creates a number stream from start to end. both start and end are included. [start, end]
|
// 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-
|
// Play: https://go.dev/play/p/9Ex1-zcg-B-
|
||||||
func FromRange[T constraints.Integer | constraints.Float](start, end, step T) stream[T] {
|
func FromRange[T constraints.Integer | constraints.Float](start, end, step T) Stream[T] {
|
||||||
if end < start {
|
if end < start {
|
||||||
panic("stream.FromRange: param start should be before param end")
|
panic("stream.FromRange: param start should be before param end")
|
||||||
} else if step <= 0 {
|
} else if step <= 0 {
|
||||||
@@ -113,7 +113,7 @@ func FromRange[T constraints.Integer | constraints.Float](start, end, step T) st
|
|||||||
|
|
||||||
// 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.
|
// 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
|
// Play: https://go.dev/play/p/HM4OlYk_OUC
|
||||||
func Concat[T any](a, b stream[T]) stream[T] {
|
func Concat[T any](a, b Stream[T]) Stream[T] {
|
||||||
source := make([]T, 0)
|
source := make([]T, 0)
|
||||||
|
|
||||||
source = append(source, a.source...)
|
source = append(source, a.source...)
|
||||||
@@ -124,7 +124,7 @@ func Concat[T any](a, b stream[T]) stream[T] {
|
|||||||
|
|
||||||
// Distinct returns a stream that removes the duplicated items.
|
// Distinct returns a stream that removes the duplicated items.
|
||||||
// Play: https://go.dev/play/p/eGkOSrm64cB
|
// Play: https://go.dev/play/p/eGkOSrm64cB
|
||||||
func (s stream[T]) Distinct() stream[T] {
|
func (s Stream[T]) Distinct() Stream[T] {
|
||||||
source := make([]T, 0)
|
source := make([]T, 0)
|
||||||
|
|
||||||
distinct := map[string]bool{}
|
distinct := map[string]bool{}
|
||||||
@@ -153,7 +153,7 @@ func hashKey(data any) string {
|
|||||||
|
|
||||||
// Filter returns a stream consisting of the elements of this stream that match the given predicate.
|
// Filter returns a stream consisting of the elements of this stream that match the given predicate.
|
||||||
// Play: https://go.dev/play/p/MFlSANo-buc
|
// Play: https://go.dev/play/p/MFlSANo-buc
|
||||||
func (s stream[T]) Filter(predicate func(item T) bool) stream[T] {
|
func (s Stream[T]) Filter(predicate func(item T) bool) Stream[T] {
|
||||||
source := make([]T, 0)
|
source := make([]T, 0)
|
||||||
|
|
||||||
for _, v := range s.source {
|
for _, v := range s.source {
|
||||||
@@ -167,7 +167,7 @@ func (s stream[T]) Filter(predicate func(item T) bool) stream[T] {
|
|||||||
|
|
||||||
// Map returns a stream consisting of the elements of this stream that apply the given function to elements of stream.
|
// 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
|
// Play: https://go.dev/play/p/OtNQUImdYko
|
||||||
func (s stream[T]) Map(mapper func(item T) T) stream[T] {
|
func (s Stream[T]) Map(mapper func(item T) T) Stream[T] {
|
||||||
source := make([]T, s.Count())
|
source := make([]T, s.Count())
|
||||||
|
|
||||||
for i, v := range s.source {
|
for i, v := range s.source {
|
||||||
@@ -179,7 +179,7 @@ func (s stream[T]) Map(mapper func(item T) 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.
|
// 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
|
// Play: https://go.dev/play/p/u1VNzHs6cb2
|
||||||
func (s stream[T]) Peek(consumer func(item T)) stream[T] {
|
func (s Stream[T]) Peek(consumer func(item T)) Stream[T] {
|
||||||
for _, v := range s.source {
|
for _, v := range s.source {
|
||||||
consumer(v)
|
consumer(v)
|
||||||
}
|
}
|
||||||
@@ -190,7 +190,7 @@ func (s stream[T]) Peek(consumer func(item 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 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.
|
// If this stream contains fewer than n elements then an empty stream will be returned.
|
||||||
// Play: https://go.dev/play/p/fNdHbqjahum
|
// Play: https://go.dev/play/p/fNdHbqjahum
|
||||||
func (s stream[T]) Skip(n int) stream[T] {
|
func (s Stream[T]) Skip(n int) Stream[T] {
|
||||||
if n <= 0 {
|
if n <= 0 {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
@@ -211,7 +211,7 @@ func (s stream[T]) Skip(n int) stream[T] {
|
|||||||
|
|
||||||
// Limit returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.
|
// 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
|
// Play: https://go.dev/play/p/qsO4aniDcGf
|
||||||
func (s stream[T]) Limit(maxSize int) stream[T] {
|
func (s Stream[T]) Limit(maxSize int) Stream[T] {
|
||||||
if s.source == nil {
|
if s.source == nil {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
@@ -231,7 +231,7 @@ func (s stream[T]) Limit(maxSize int) stream[T] {
|
|||||||
|
|
||||||
// AllMatch returns whether all elements of this stream match the provided predicate.
|
// AllMatch returns whether all elements of this stream match the provided predicate.
|
||||||
// Play: https://go.dev/play/p/V5TBpVRs-Cx
|
// Play: https://go.dev/play/p/V5TBpVRs-Cx
|
||||||
func (s stream[T]) AllMatch(predicate func(item T) bool) bool {
|
func (s Stream[T]) AllMatch(predicate func(item T) bool) bool {
|
||||||
for _, v := range s.source {
|
for _, v := range s.source {
|
||||||
if !predicate(v) {
|
if !predicate(v) {
|
||||||
return false
|
return false
|
||||||
@@ -243,7 +243,7 @@ func (s stream[T]) AllMatch(predicate func(item T) bool) bool {
|
|||||||
|
|
||||||
// AnyMatch returns whether any elements of this stream match the provided predicate.
|
// AnyMatch returns whether any elements of this stream match the provided predicate.
|
||||||
// Play: https://go.dev/play/p/PTCnWn4OxSn
|
// Play: https://go.dev/play/p/PTCnWn4OxSn
|
||||||
func (s stream[T]) AnyMatch(predicate func(item T) bool) bool {
|
func (s Stream[T]) AnyMatch(predicate func(item T) bool) bool {
|
||||||
for _, v := range s.source {
|
for _, v := range s.source {
|
||||||
if predicate(v) {
|
if predicate(v) {
|
||||||
return true
|
return true
|
||||||
@@ -255,13 +255,13 @@ func (s stream[T]) AnyMatch(predicate func(item T) bool) bool {
|
|||||||
|
|
||||||
// NoneMatch returns whether no elements of this stream match the provided predicate.
|
// NoneMatch returns whether no elements of this stream match the provided predicate.
|
||||||
// Play: https://go.dev/play/p/iWS64pL1oo3
|
// Play: https://go.dev/play/p/iWS64pL1oo3
|
||||||
func (s stream[T]) NoneMatch(predicate func(item T) bool) bool {
|
func (s Stream[T]) NoneMatch(predicate func(item T) bool) bool {
|
||||||
return !s.AnyMatch(predicate)
|
return !s.AnyMatch(predicate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ForEach performs an action for each element of this stream.
|
// ForEach performs an action for each element of this stream.
|
||||||
// Play: https://go.dev/play/p/Dsm0fPqcidk
|
// Play: https://go.dev/play/p/Dsm0fPqcidk
|
||||||
func (s stream[T]) ForEach(action func(item T)) {
|
func (s Stream[T]) ForEach(action func(item T)) {
|
||||||
for _, v := range s.source {
|
for _, v := range s.source {
|
||||||
action(v)
|
action(v)
|
||||||
}
|
}
|
||||||
@@ -269,7 +269,7 @@ func (s stream[T]) ForEach(action func(item 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.
|
// 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
|
// Play: https://go.dev/play/p/6uzZjq_DJLU
|
||||||
func (s stream[T]) Reduce(initial T, accumulator func(a, b T) T) T {
|
func (s Stream[T]) Reduce(initial T, accumulator func(a, b T) T) T {
|
||||||
for _, v := range s.source {
|
for _, v := range s.source {
|
||||||
initial = accumulator(initial, v)
|
initial = accumulator(initial, v)
|
||||||
}
|
}
|
||||||
@@ -279,13 +279,13 @@ func (s stream[T]) Reduce(initial T, accumulator func(a, b T) T) T {
|
|||||||
|
|
||||||
// Count returns the count of elements in the stream.
|
// Count returns the count of elements in the stream.
|
||||||
// Play: https://go.dev/play/p/r3koY6y_Xo-
|
// Play: https://go.dev/play/p/r3koY6y_Xo-
|
||||||
func (s stream[T]) Count() int {
|
func (s Stream[T]) Count() int {
|
||||||
return len(s.source)
|
return len(s.source)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindFirst returns the first element of this stream and true, or zero value and false if the stream is empty.
|
// 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
|
// Play: https://go.dev/play/p/9xEf0-6C1e3
|
||||||
func (s stream[T]) FindFirst() (T, bool) {
|
func (s Stream[T]) FindFirst() (T, bool) {
|
||||||
var result T
|
var result T
|
||||||
|
|
||||||
if s.source == nil || len(s.source) == 0 {
|
if s.source == nil || len(s.source) == 0 {
|
||||||
@@ -297,7 +297,7 @@ func (s stream[T]) FindFirst() (T, bool) {
|
|||||||
|
|
||||||
// FindLast returns the last element of this stream and true, or zero value and false if the stream is empty.
|
// 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
|
// Play: https://go.dev/play/p/WZD2rDAW-2h
|
||||||
func (s stream[T]) FindLast() (T, bool) {
|
func (s Stream[T]) FindLast() (T, bool) {
|
||||||
var result T
|
var result T
|
||||||
|
|
||||||
if s.source == nil || len(s.source) == 0 {
|
if s.source == nil || len(s.source) == 0 {
|
||||||
@@ -309,7 +309,7 @@ func (s stream[T]) FindLast() (T, bool) {
|
|||||||
|
|
||||||
// Reverse returns a stream whose elements are reverse order of given stream.
|
// Reverse returns a stream whose elements are reverse order of given stream.
|
||||||
// Play: https://go.dev/play/p/A8_zkJnLHm4
|
// Play: https://go.dev/play/p/A8_zkJnLHm4
|
||||||
func (s stream[T]) Reverse() stream[T] {
|
func (s Stream[T]) Reverse() Stream[T] {
|
||||||
l := len(s.source)
|
l := len(s.source)
|
||||||
source := make([]T, l)
|
source := make([]T, l)
|
||||||
|
|
||||||
@@ -321,7 +321,7 @@ func (s stream[T]) Reverse() stream[T] {
|
|||||||
|
|
||||||
// Range returns a stream whose elements are in the range from start(included) to end(excluded) original stream.
|
// 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
|
// Play: https://go.dev/play/p/indZY5V2f4j
|
||||||
func (s stream[T]) Range(start, end int) stream[T] {
|
func (s Stream[T]) Range(start, end int) Stream[T] {
|
||||||
if start < 0 {
|
if start < 0 {
|
||||||
start = 0
|
start = 0
|
||||||
}
|
}
|
||||||
@@ -347,7 +347,7 @@ func (s stream[T]) Range(start, end int) stream[T] {
|
|||||||
|
|
||||||
// Sorted returns a stream consisting of the elements of this stream, sorted according to the provided less function.
|
// 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
|
// Play: https://go.dev/play/p/XXtng5uonFj
|
||||||
func (s stream[T]) Sorted(less func(a, b T) bool) stream[T] {
|
func (s Stream[T]) Sorted(less func(a, b T) bool) Stream[T] {
|
||||||
source := []T{}
|
source := []T{}
|
||||||
source = append(source, s.source...)
|
source = append(source, s.source...)
|
||||||
|
|
||||||
@@ -359,7 +359,7 @@ func (s stream[T]) Sorted(less func(a, b T) bool) stream[T] {
|
|||||||
// Max returns the maximum element of this stream according to the provided less function.
|
// Max returns the maximum element of this stream according to the provided less function.
|
||||||
// less: a > b
|
// less: a > b
|
||||||
// Play: https://go.dev/play/p/fm-1KOPtGzn
|
// Play: https://go.dev/play/p/fm-1KOPtGzn
|
||||||
func (s stream[T]) Max(less func(a, b T) bool) (T, bool) {
|
func (s Stream[T]) Max(less func(a, b T) bool) (T, bool) {
|
||||||
var max T
|
var max T
|
||||||
|
|
||||||
if len(s.source) == 0 {
|
if len(s.source) == 0 {
|
||||||
@@ -377,7 +377,7 @@ func (s stream[T]) Max(less func(a, b T) bool) (T, bool) {
|
|||||||
// Min returns the minimum element of this stream according to the provided less function.
|
// Min returns the minimum element of this stream according to the provided less function.
|
||||||
// less: a < b
|
// less: a < b
|
||||||
// Play: https://go.dev/play/p/vZfIDgGNRe_0
|
// Play: https://go.dev/play/p/vZfIDgGNRe_0
|
||||||
func (s stream[T]) Min(less func(a, b T) bool) (T, bool) {
|
func (s Stream[T]) Min(less func(a, b T) bool) (T, bool) {
|
||||||
var min T
|
var min T
|
||||||
|
|
||||||
if len(s.source) == 0 {
|
if len(s.source) == 0 {
|
||||||
@@ -395,6 +395,6 @@ func (s stream[T]) Min(less func(a, b T) bool) (T, bool) {
|
|||||||
|
|
||||||
// ToSlice return the elements in the stream.
|
// ToSlice return the elements in the stream.
|
||||||
// Play: https://go.dev/play/p/jI6_iZZuVFE
|
// Play: https://go.dev/play/p/jI6_iZZuVFE
|
||||||
func (s stream[T]) ToSlice() []T {
|
func (s Stream[T]) ToSlice() []T {
|
||||||
return s.source
|
return s.source
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user