-
Notifications
You must be signed in to change notification settings - Fork 3
/
numeric.go
93 lines (73 loc) · 1.88 KB
/
numeric.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package collections
import (
"github.com/thefuga/go-collections/errors"
"github.com/thefuga/go-collections/internal"
)
// Sum sums all the values stored on the numeric slice and returns the result.
func Sum[T internal.Number](slice []T) T {
var sum T
for _, v := range slice {
sum += v
}
return sum
}
// AverageE calculates the average value of the slice. Should the slice be empty,
// an instance of errors.EmptyCollectionError is returned.
func AverageE[T internal.Number](slice []T) (T, error) {
if len(slice) == 0 {
return *new(T), errors.NewEmptyCollectionError()
}
return Sum(slice) / T(len(slice)), nil
}
// Average uses AverageE, omitting the error.
func Average[T internal.Number](slice []T) T {
avg, _ := AverageE(slice)
return avg
}
// MinE returns the minimal value stored on the numeric slice. Should the slice be
// empty, an error is returned.
func MinE[T internal.Number](slice []T) (T, error) {
min, err := FirstE(slice)
if err != nil {
return min, err
}
for _, v := range slice {
if v < min {
min = v
}
}
return min, nil
}
// Min uses MinE, omitting the error.
func Min[T internal.Number](slice []T) T {
min, _ := MinE(slice)
return min
}
// MaxE returns the maximum value stored on the numeric slice. Should the slice be
// empty, an error is returned.
func MaxE[T internal.Number](slice []T) (T, error) {
max, err := FirstE(slice)
if err != nil {
return max, err
}
for _, v := range slice {
if v > max {
max = v
}
}
return max, nil
}
// Max uses MaxE, omitting the error.
func Max[T internal.Number](slice []T) T {
max, _ := MaxE(slice)
return max
}
// Median calculates and returns the median value of the slice.
func Median[T internal.Number](slice []T) float64 {
Sort(slice, Asc[T]())
halfway := len(slice) / 2
if len(slice)%2 == 0 {
return float64(slice[halfway]+slice[halfway-1]) / 2.0
}
return float64(slice[halfway])
}