1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

doc: add document for stream package

This commit is contained in:
dudaodong
2023-04-05 20:17:41 +08:00
parent 5b9b4c4344
commit c53d541a6b
4 changed files with 1822 additions and 10 deletions

View File

@@ -51,7 +51,7 @@ type stream[T any] struct {
source []T
}
// Of creates a stream 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
func Of[T any](elems ...T) stream[T] {
return FromSlice(elems)
@@ -268,7 +268,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.
// Play: https://go.dev/play/p/g-xkHiuIpTi
// Play: https://go.dev/play/p/6uzZjq_DJLU
func (s stream[T]) Reduce(initial T, accumulator func(a, b T) T) T {
for _, v := range s.source {
initial = accumulator(initial, v)

View File

@@ -91,11 +91,11 @@ func ExampleConcat() {
}
func ExampleStream_Distinct() {
originalStream := FromSlice([]int{1, 2, 2, 3, 3, 3})
distinctStream := originalStream.Distinct()
original := FromSlice([]int{1, 2, 2, 3, 3, 3})
distinct := original.Distinct()
data1 := originalStream.ToSlice()
data2 := distinctStream.ToSlice()
data1 := original.ToSlice()
data2 := distinct.ToSlice()
fmt.Println(data1)
fmt.Println(data2)
@@ -267,10 +267,8 @@ func ExampleStream_ForEach() {
func ExampleStream_Reduce() {
original := FromSlice([]int{1, 2, 3})
result := 0
original.ForEach(func(item int) {
result += item
result := original.Reduce(0, func(a, b int) int {
return a + b
})
fmt.Println(result)