1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 23:22:28 +08:00

feat: add Skip for stream

This commit is contained in:
dudaodong
2023-01-30 16:56:33 +08:00
parent 82cbb54787
commit ea0f96a8c0
2 changed files with 64 additions and 1 deletions

View File

@@ -28,7 +28,7 @@ import (
// Min(less func(a, b T) bool) (T, bool)
// Limit(maxSize int) StreamI[T]
// Skip(n int64) StreamI[T]
// Skip(n int) StreamI[T]
// AllMatch(predicate func(item T) bool) bool
// AnyMatch(predicate func(item T) bool) bool
@@ -158,6 +158,37 @@ func (s stream[T]) Map(mapper func(item T) T) stream[T] {
return FromSlice(source)
}
// 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.
func (s stream[T]) Peek(consumer func(item T)) stream[T] {
for _, v := range s.source {
consumer(v)
}
return s
}
// 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.
func (s stream[T]) Skip(n int) stream[T] {
if n <= 0 {
return s
}
source := make([]T, 0)
l := len(s.source)
if n > l {
return FromSlice(source)
}
// source = make([]T, l-n+1, l-n+1)
for i := n; i < l; i++ {
source = append(source, s.source[i])
}
return FromSlice(source)
}
// Count returns the count of elements in the stream.
func (s stream[T]) Count() int {
return len(s.source)