Skip to content

Commit 2bdcaca

Browse files
committed
doc: add example for Product and ProductBy
1 parent 1c1dfd9 commit 2bdcaca

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

math.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Comple
8888
// Product gets the product of the values in a collection. If collection is empty 0 is returned.
8989
// Play: https://go.dev/play/p/2_kjM_smtAH
9090
func Product[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) T {
91-
9291
if collection == nil {
9392
return 0
9493
}
@@ -107,7 +106,6 @@ func Product[T constraints.Float | constraints.Integer | constraints.Complex](co
107106
// ProductBy summarizes the values in a collection using the given return value from the iteration function. If collection is empty 0 is returned.
108107
// Play: https://go.dev/play/p/wadzrWr9Aer
109108
func ProductBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) R) R {
110-
111109
if collection == nil {
112110
return 0
113111
}

math_example_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,33 @@ func ExampleSumBy() {
6767
// Output: 6
6868
}
6969

70+
func ExampleProduct() {
71+
list := []int{1, 2, 3, 4, 5}
72+
73+
result := Product(list)
74+
75+
fmt.Printf("%v", result)
76+
// Output: 120
77+
}
78+
79+
func ExampleProductBy() {
80+
list := []string{"foo", "bar"}
81+
82+
result := ProductBy(list, func(item string) int {
83+
return len(item)
84+
})
85+
86+
fmt.Printf("%v", result)
87+
// Output: 9
88+
}
89+
7090
func ExampleMean() {
7191
list := []int{1, 2, 3, 4, 5}
7292

7393
result := Mean(list)
7494

7595
fmt.Printf("%v", result)
96+
// Output: 3
7697
}
7798

7899
func ExampleMeanBy() {
@@ -83,4 +104,5 @@ func ExampleMeanBy() {
83104
})
84105

85106
fmt.Printf("%v", result)
107+
// Output: 3
86108
}

0 commit comments

Comments
 (0)