-
-
Notifications
You must be signed in to change notification settings - Fork 453
/
generic.go
108 lines (91 loc) · 2.48 KB
/
generic.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import "fmt"
// Int is a type constraint. It can be used to indicate that a function can accept the
// types listed within the constraint, in this case int, int32 and int64.
type Int interface {
int | int32 | int64
}
// Float is also a type constraint but describe type float32 and float64.
type Float interface {
float32 | float64
}
// The Number used the Int and Float constraint. In this case it has the same effect as
//
// type Number interface {
// int | int32 | int64 | float32 | float64
// }
type Number interface {
Int | Float
}
func main() {
// Initialize a map for the integer values
ints := []int64{6, 3, 555}
// Initialize a map for the float values
floats := []float64{35.98, 26.99, 933.0001, 3.14}
fmt.Printf("(Non-Generic) SumInts: %v, SumFloats: %v\n",
SumInts(ints),
SumFloats(floats))
fmt.Printf("(type explicitly specified) SumIntsOrFloats: %v, SumIntsOrFloats: %v\n",
SumIntsOrFloats[int64](ints),
SumIntsOrFloats[float64](floats))
fmt.Printf("(type infered) SumIntsOrFloats: %v, SumIntsOrFloats: %v\n",
SumIntsOrFloats(ints),
SumIntsOrFloats(floats))
fmt.Printf("(With Constraint) SumIntsGeneric: %v, SumIntsGeneric: %v\n",
SumIntsGeneric(ints),
SumFloatsGeneric(floats))
fmt.Printf("(With Constraint) SumNumbers: %v, SumNumbers: %v\n",
SumNumbers(ints),
SumNumbers(floats))
}
// SumInts sums the values in s. (Non-Generic)
func SumInts(s []int64) int64 {
var sum int64
for _, v := range s {
sum += v
}
return sum
}
// SumFloats sums the values in s. (Non-Generic)
func SumFloats(s []float64) float64 {
var sum float64
for _, v := range s {
sum += v
}
return sum
}
// SumIntsGeneric sums the values in s. It supports both Int slice. (Generic)
func SumIntsGeneric[V Int](s []V) V {
var sum V
for _, v := range s {
sum += v
}
return sum
}
// SumFloatsGeneric sums the values in s. It supports both Float slice. (Generic)
func SumFloatsGeneric[V Float](s []V) V {
var sum V
for _, v := range s {
sum += v
}
return sum
}
// SumIntsOrFloats sums the values in slice s. It supports both int64 and float64 slice. (Generic)
func SumIntsOrFloats[V int64 | float64](s []V) V {
var sum V
for _, v := range s {
sum += v
}
return sum
}
// SumNumbers sums the values in s. Its supports any type that fulfill Number constraint. (Generic)
// Note: This function header can also be written as
//
// func SumNumbers[V Int | Float](s []V) V
func SumNumbers[V Number](s []V) V {
var sum V
for _, v := range s {
sum += v
}
return sum
}