forked from rai-project/dlframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
56 lines (48 loc) · 1.07 KB
/
utils.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
package dlframework
import (
strings "strings"
"golang.org/x/sync/syncmap"
)
func syncMapKeys(m *syncmap.Map) []string {
keys := []string{}
m.Range(func(key0 interface{}, value interface{}) bool {
key, ok := key0.(string)
if !ok {
return true
}
keys = append(keys, key)
return true
})
return keys
}
func CleanString(str string) string {
r := strings.NewReplacer(" ", "_", "-", "_")
res := r.Replace(str)
return strings.ToLower(res)
}
func Partition(in []interface{}, partitionSize int) (out [][]interface{}) {
cnt := (len(in)-1)/partitionSize + 1
for i := 0; i < cnt; i++ {
start := i * partitionSize
end := (i + 1) * partitionSize
if end > len(in) {
end = len(in)
}
part := in[start:end]
out = append(out, part)
}
return out
}
func PartitionStringList(in []string, partitionSize int) (out [][]string) {
cnt := (len(in)-1)/partitionSize + 1
for ii := 0; ii < cnt; ii++ {
start := ii * partitionSize
end := (ii + 1) * partitionSize
if end > len(in) {
end = len(in)
}
part := in[start:end]
out = append(out, part)
}
return out
}