-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate.go
83 lines (74 loc) · 1.6 KB
/
aggregate.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
package godino
import (
"errors"
"golang.org/x/exp/constraints"
)
type Number interface {
constraints.Integer | constraints.Float | constraints.Complex
}
// Returns true if all of the given conditions are true
func All(conditions ...bool) bool {
for _, c := range conditions {
if !c {
return false
}
}
return true
}
// Returns true if at least one of the given conditions are true
func Any(conditions ...bool) bool {
for _, c := range conditions {
if c {
return true
}
}
return false
}
// Returns the highest value of the given arguments. Returns an error if no
// arguments are given.
func Max[T constraints.Ordered](elements ...T) (T, error) {
var max T
var err error
if len(elements) == 0 {
err = errors.New("Max() expected at least 1 argument, got 0")
} else {
for i, e := range elements {
if i == 0 || e > max {
max = e
}
}
}
return max, err
}
// Returns the lowest value of the given arguments. Returns an error if no
// arguments are given.
func Min[T constraints.Ordered](elements ...T) (T, error) {
var min T
var err error
if len(elements) == 0 {
err = errors.New("Min() expected at least 1 argument, got 0")
} else {
for i, e := range elements {
if i == 0 || e < min {
min = e
}
}
}
return min, err
}
// Multiplies the given numeric arguments and returns the product
func Prod[T Number](numbers ...T) T {
product := T(1)
for _, n := range numbers {
product *= n
}
return product
}
// Adds the given numeric arguments and returns the sum
func Sum[T Number](numbers ...T) T {
var sum T
for _, n := range numbers {
sum += n
}
return sum
}