-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart_generic.go
58 lines (51 loc) · 1.03 KB
/
part_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
package mc
func SliceContains[V comparable](slice []V, v V) bool {
for _, s := range slice {
if s == v {
return true
}
}
return false
}
func SliceFilter[V any](slice []V, f func(V) bool) []V {
var result []V
for _, s := range slice {
if f(s) {
result = append(result, s)
}
}
return result
}
func SliceMap[V any, R any](slice []V, f func(V) R) []R {
var result []R
for _, s := range slice {
result = append(result, f(s))
}
return result
}
// PointerTo creates a pointer to bool
func PointerTo[V any](v V) *V {
return &v
}
func PtrTo[V any](v V) *V {
return PointerTo(v)
}
// ValueOr returns the default value if the value is empty
func ValueOr[V comparable](v V, alternatives ...V) V {
zero := new(V)
if v == *zero {
if len(alternatives) == 0 {
return *zero
}
for _, alternative := range alternatives {
if alternative != *zero {
return alternative
}
}
return alternatives[len(alternatives)-1]
}
return v
}
func VarOr[V comparable](v V, alternatives ...V) V {
return ValueOr(v, alternatives...)
}