-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgo2.go
60 lines (50 loc) · 931 Bytes
/
go2.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
// Package go2 contains general utility helpers that should've been in Go. Maybe they'll be in Go 2.0.
package go2
import (
"hash/fnv"
"math"
"golang.org/x/exp/constraints"
)
func Pointer[T any](v T) *T {
return &v
}
func Min[T constraints.Ordered](a, b T) T {
if a < b {
return a
}
return b
}
func Max[T constraints.Ordered](a, b T) T {
if a > b {
return a
}
return b
}
func StringToIntHash(s string) int {
h := fnv.New32a()
h.Write([]byte(s))
return int(h.Sum32())
}
func Contains[T comparable](els []T, el T) bool {
for _, el2 := range els {
if el2 == el {
return true
}
}
return false
}
func Filter[T any](els []T, fn func(T) bool) []T {
out := []T{}
for _, el := range els {
if fn(el) {
out = append(out, el)
}
}
return out
}
func IntMax(x, y int) int {
return int(math.Max(float64(x), float64(y)))
}
func IntMin(x, y int) int {
return int(math.Min(float64(x), float64(y)))
}