-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxlo.go
52 lines (45 loc) · 1.66 KB
/
xlo.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
package xlo
import (
"github.com/samber/lo"
lop "github.com/samber/lo/parallel"
)
// Maximum average load of a bucket that triggers growth is 6.5.
// Represent as loadFactorNum/loadFactorDen, to allow integer math.
// loadFactorDen = 2
// defined in runtime./map.go
const loadFactorNum = 13
// Uniq returns a duplicate-free version of an array, in which only the first occurrence of each element is kept.
// The order of result values is determined by the order they occur in the array.
//
// Some optimizations for small capacity temp map, if capacity less than loadFactorNum.
func Uniq[T comparable](collection []T) []T {
size := len(collection)
result := make([]T, 0, size)
var temp map[T]struct{}
if size < loadFactorNum {
temp = map[T]struct{}{}
} else {
temp = make(map[T]struct{}, size)
}
for _, item := range collection {
if _, ok := temp[item]; !ok {
temp[item] = struct{}{}
result = append(result, item)
}
}
return result
}
// LoUniq returns a duplicate-free version of an array, in which only the first occurrence of each element is kept.
// The order of result values is determined by the order they occur in the array.
func LoUniq[T comparable](collection []T) []T {
return lo.Uniq(collection)
}
// LoMap manipulates a slice and transforms it to a slice of another type.
func LoMap[T any, R any](collection []T, iteratee func(item T, index int) R) []R {
return lo.Map(collection, iteratee)
}
// LopMap manipulates a slice and transforms it to a slice of another type.
// `iteratee` is call in parallel. Result keep the same order.
func LopMap[T any, R any](collection []T, iteratee func(item T, index int) R) []R {
return lop.Map(collection, iteratee)
}