-
Notifications
You must be signed in to change notification settings - Fork 761
/
util.go
185 lines (166 loc) · 4.94 KB
/
util.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// util contains small standalone utility functions. This should have no
// dependencies on other server packages.
package util
import (
"context"
"sort"
"time"
"golang.org/x/exp/constraints"
"golang.org/x/exp/maps"
)
// MinTime returns the earlier of two given time.Time
func MinTime(a, b time.Time) time.Time {
if a.Before(b) {
return a
}
return b
}
// MaxTime returns the later of two given time.Time
func MaxTime(a, b time.Time) time.Time {
if a.After(b) {
return a
}
return b
}
// NextAlignedTime returns the earliest time after `t` that is aligned to an integer multiple
// of `align` since the unix epoch.
func NextAlignedTime(t time.Time, align time.Duration) time.Time {
return time.Unix(0, (t.UnixNano()/int64(align)+1)*int64(align))
}
// SortSlice sorts the given slice of an ordered type.
// Sort is not guaranteed to be stable.
func SortSlice[S ~[]E, E constraints.Ordered](slice S) {
sort.Slice(slice, func(i, j int) bool {
return slice[i] < slice[j]
})
}
// SliceHead returns the first n elements of s. n may be greater than len(s).
func SliceHead[S ~[]E, E any](s S, n int) S {
if n < len(s) {
return s[:n]
}
return s
}
// SliceTail returns the last n elements of s. n may be greater than len(s).
func SliceTail[S ~[]E, E any](s S, n int) S {
if extra := len(s) - n; extra > 0 {
return s[extra:]
}
return s
}
// CloneMapNonNil is like maps.Clone except it can't return nil, it will return an empty map instead.
func CloneMapNonNil[M ~map[K]V, K comparable, V any](m M) M {
m = maps.Clone(m)
if m == nil {
m = make(M)
}
return m
}
// InverseMap creates the inverse map, ie., for a key-value map, it builds the value-key map.
func InverseMap[M ~map[K]V, K, V comparable](m M) map[V]K {
if m == nil {
return nil
}
invm := make(map[V]K, len(m))
for k, v := range m {
invm[v] = k
}
return invm
}
// MapConcurrent concurrently maps a function over input and fails fast on error.
func MapConcurrent[IN any, OUT any](input []IN, mapper func(IN) (OUT, error)) ([]OUT, error) {
errorsCh := make(chan error, len(input))
results := make([]OUT, len(input))
for i, in := range input {
i := i
in := in
go func() {
var err error
results[i], err = mapper(in)
errorsCh <- err
}()
}
for range input {
if err := <-errorsCh; err != nil {
return nil, err
}
}
return results, nil
}
// MapSlice given slice xs []T and f(T) S produces slice []S by applying f to every element of xs
func MapSlice[T, S any](xs []T, f func(T) S) []S {
if xs == nil {
return nil
}
result := make([]S, len(xs))
for i, s := range xs {
result[i] = f(s)
}
return result
}
// FilterSlice iterates over elements of a slice, returning a new slice of all elements predicate returns true for.
func FilterSlice[T any](in []T, predicate func(T) bool) []T {
var out []T
for _, elem := range in {
if predicate(elem) {
out = append(out, elem)
}
}
return out
}
// FoldSlice folds left a slice using given reducer function and initial value.
func FoldSlice[T any, A any](in []T, initializer A, reducer func(A, T) A) A {
acc := initializer
for _, val := range in {
acc = reducer(acc, val)
}
return acc
}
// RepeatSlice given slice and a number (n) produces a new slice containing original slice n times
// if n is non-positive will produce nil
func RepeatSlice[T any](xs []T, n int) []T {
if xs == nil || n <= 0 {
return nil
}
ys := make([]T, n*len(xs))
for i := 0; i < n; i++ {
copy(ys[i*len(xs):], xs)
}
return ys
}
// Ptr returns a pointer to a copy of v.
func Ptr[T any](v T) *T {
return &v
}
// InterruptibleSleep is like time.Sleep but can be interrupted by a context.
func InterruptibleSleep(ctx context.Context, timeout time.Duration) {
timer := time.NewTimer(timeout)
defer timer.Stop()
select {
case <-timer.C:
case <-ctx.Done():
}
}