Skip to content

Commit 97074ee

Browse files
authored
Adding Mean and MeanBy (#414)
* feat: create Mean for ints and floats * feat: create MeanBy for ints and floats * chore: add example tests for mean Mean and MeanBy
1 parent 05a9bc9 commit 97074ee

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

math.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,23 @@ func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Comple
8282
}
8383
return sum
8484
}
85+
86+
// Mean calculates the mean of a collection of numbers.
87+
func Mean[T constraints.Float | constraints.Integer](collection []T) T {
88+
var length T = T(len(collection))
89+
if length == 0 {
90+
return 0
91+
}
92+
var sum T = Sum(collection)
93+
return sum / length
94+
}
95+
96+
// MeanBy calculates the mean of a collection of numbers using the given return value from the iteration function.
97+
func MeanBy[T any, R constraints.Float | constraints.Integer](collection []T, iteratee func(item T) R) R {
98+
var length R = R(len(collection))
99+
if length == 0 {
100+
return 0
101+
}
102+
var sum R = SumBy(collection, iteratee)
103+
return sum / length
104+
}

math_example_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,21 @@ func ExampleSumBy() {
6666
fmt.Printf("%v", result)
6767
// Output: 6
6868
}
69+
70+
func ExampleMean() {
71+
list := []int{1, 2, 3, 4, 5}
72+
73+
result := Mean(list)
74+
75+
fmt.Printf("%v", result)
76+
}
77+
78+
func ExampleMeanBy() {
79+
list := []string{"foo", "bar"}
80+
81+
result := MeanBy(list, func(item string) int {
82+
return len(item)
83+
})
84+
85+
fmt.Printf("%v", result)
86+
}

math_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,33 @@ func TestSumBy(t *testing.T) {
9797
is.Equal(result4, uint32(0))
9898
is.Equal(result5, complex128(6_6))
9999
}
100+
101+
func TestMean(t *testing.T) {
102+
t.Parallel()
103+
is := assert.New(t)
104+
105+
result1 := Mean([]float32{2.3, 3.3, 4, 5.3})
106+
result2 := Mean([]int32{2, 3, 4, 5})
107+
result3 := Mean([]uint32{2, 3, 4, 5})
108+
result4 := Mean([]uint32{})
109+
110+
is.Equal(result1, float32(3.7250001))
111+
is.Equal(result2, int32(3))
112+
is.Equal(result3, uint32(3))
113+
is.Equal(result4, uint32(0))
114+
}
115+
116+
func TestMeanBy(t *testing.T) {
117+
t.Parallel()
118+
is := assert.New(t)
119+
120+
result1 := MeanBy([]float32{2.3, 3.3, 4, 5.3}, func(n float32) float32 { return n })
121+
result2 := MeanBy([]int32{2, 3, 4, 5}, func(n int32) int32 { return n })
122+
result3 := MeanBy([]uint32{2, 3, 4, 5}, func(n uint32) uint32 { return n })
123+
result4 := MeanBy([]uint32{}, func(n uint32) uint32 { return n })
124+
125+
is.Equal(result1, float32(3.7250001))
126+
is.Equal(result2, int32(3))
127+
is.Equal(result3, uint32(3))
128+
is.Equal(result4, uint32(0))
129+
}

0 commit comments

Comments
 (0)