doc: add example for Product and ProductBy

This commit is contained in:
Samuel Berthe
2025-01-27 01:05:21 +01:00
parent 1c1dfd9d29
commit 2bdcacae5e
2 changed files with 22 additions and 2 deletions

View File

@@ -88,7 +88,6 @@ func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Comple
// Product gets the product of the values in a collection. If collection is empty 0 is returned.
// Play: https://go.dev/play/p/2_kjM_smtAH
func Product[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) T {
if collection == nil {
return 0
}
@@ -107,7 +106,6 @@ func Product[T constraints.Float | constraints.Integer | constraints.Complex](co
// ProductBy summarizes the values in a collection using the given return value from the iteration function. If collection is empty 0 is returned.
// Play: https://go.dev/play/p/wadzrWr9Aer
func ProductBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) R) R {
if collection == nil {
return 0
}

View File

@@ -67,12 +67,33 @@ func ExampleSumBy() {
// Output: 6
}
func ExampleProduct() {
list := []int{1, 2, 3, 4, 5}
result := Product(list)
fmt.Printf("%v", result)
// Output: 120
}
func ExampleProductBy() {
list := []string{"foo", "bar"}
result := ProductBy(list, func(item string) int {
return len(item)
})
fmt.Printf("%v", result)
// Output: 9
}
func ExampleMean() {
list := []int{1, 2, 3, 4, 5}
result := Mean(list)
fmt.Printf("%v", result)
// Output: 3
}
func ExampleMeanBy() {
@@ -83,4 +104,5 @@ func ExampleMeanBy() {
})
fmt.Printf("%v", result)
// Output: 3
}