-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
72 lines (61 loc) · 1.71 KB
/
utils.go
File metadata and controls
72 lines (61 loc) · 1.71 KB
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
// Copyright (C) 2025, Lux Partners Limited All rights reserved.
// See the file LICENSE for licensing terms.
package utils
import (
"context"
"os"
"os/signal"
"slices"
"time"
"github.com/luxfi/constants"
)
// Unique returns a new slice containing only the unique elements from the input slice.
func Unique[T comparable](arr []T) []T {
visited := map[T]bool{}
unique := []T{}
for _, e := range arr {
if !visited[e] {
unique = append(unique, e)
visited[e] = true
}
}
return unique
}
func Belongs[T comparable](input []T, elem T) bool {
return slices.Contains(input, elem)
}
func Map[T, U any](input []T, f func(T) U) []U {
output := make([]U, 0, len(input))
for _, e := range input {
output = append(output, f(e))
}
return output
}
func Uint32Sort(arr []uint32) {
slices.Sort(arr)
}
// GetAPIContext returns a context for API requests.
func GetAPIContext() (context.Context, context.CancelFunc) {
return GetTimedContext(constants.APIRequestTimeout)
}
// GetAPILargeContext returns a context for API requests with large timeout.
func GetAPILargeContext() (context.Context, context.CancelFunc) {
return GetTimedContext(constants.APIRequestLargeTimeout)
}
// GetTimedContext returns a context with the given timeout.
func GetTimedContext(timeout time.Duration) (context.Context, context.CancelFunc) {
parent, sigCancel := signal.NotifyContext(context.Background(), os.Interrupt)
ctx, timeCancel := context.WithTimeout(parent, timeout)
return ctx, func() {
sigCancel()
timeCancel()
}
}
// PointersSlice converts a slice of values to a slice of pointers
func PointersSlice[T any](values []T) []*T {
pointers := make([]*T, len(values))
for i := range values {
pointers[i] = &values[i]
}
return pointers
}