mirror of
https://github.com/samber/lo.git
synced 2025-09-26 20:11:13 +08:00
doc: add example for Product and ProductBy
This commit is contained in:
2
math.go
2
math.go
@@ -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
|
||||
}
|
||||
|
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user