mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
feat:addFindLast for stream (#95)
Co-authored-by: sunyaoyao <sunyaoyao@kezaihui.com>
This commit is contained in:
@@ -295,6 +295,18 @@ func (s stream[T]) FindFirst() (T, bool) {
|
|||||||
return s.source[0], true
|
return s.source[0], true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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/9xEf0-6C1e3
|
||||||
|
func (s stream[T]) FindLast() (T, bool) {
|
||||||
|
var result T
|
||||||
|
|
||||||
|
if s.source == nil || len(s.source) == 0 {
|
||||||
|
return result, false
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.source[len(s.source)-1], true
|
||||||
|
}
|
||||||
|
|
||||||
// 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] {
|
||||||
|
|||||||
@@ -290,6 +290,19 @@ func ExampleStream_FindFirst() {
|
|||||||
// true
|
// true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleStream_FindLast() {
|
||||||
|
original := FromSlice([]int{3, 2, 1})
|
||||||
|
|
||||||
|
result, ok := original.FindLast()
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
fmt.Println(ok)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleStream_Reverse() {
|
func ExampleStream_Reverse() {
|
||||||
original := FromSlice([]int{1, 2, 3})
|
original := FromSlice([]int{1, 2, 3})
|
||||||
|
|
||||||
|
|||||||
@@ -273,6 +273,24 @@ func TestStream_FindFirst(t *testing.T) {
|
|||||||
assert.Equal(true, ok)
|
assert.Equal(true, ok)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStream_FindLast(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestStream_FindLast")
|
||||||
|
|
||||||
|
stream := FromSlice([]int{3, 2, 1})
|
||||||
|
|
||||||
|
result, ok := stream.FindLast()
|
||||||
|
|
||||||
|
assert.Equal(1, result)
|
||||||
|
assert.Equal(true, ok)
|
||||||
|
|
||||||
|
stream2 := FromSlice([]int{})
|
||||||
|
|
||||||
|
result, ok = stream2.FindLast()
|
||||||
|
|
||||||
|
assert.Equal(0, result)
|
||||||
|
assert.Equal(false, ok)
|
||||||
|
}
|
||||||
|
|
||||||
func TestStream_Reverse(t *testing.T) {
|
func TestStream_Reverse(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestStream_Reverse")
|
assert := internal.NewAssert(t, "TestStream_Reverse")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user