-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcs.go
64 lines (58 loc) · 1.87 KB
/
funcs.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
package slice
import "math/bits"
// GroupBy returns a map where elements with the same key are collected together in a slice.
func GroupBy[K comparable, V any](values []V, key func(V) K) map[K][]V {
groups := make(map[K][]V)
for _, v := range values {
k := key(v)
groups[k] = append(groups[k], v)
}
return groups
}
// Uniq returns a slice with all the unique values from the input slice, in the order they appear.
func Uniq[T comparable](slice []T) []T {
sz := simpleLog(len(slice))
dict := make(map[T]struct{}, sz)
var result []T
for _, t := range slice {
if _, seen := dict[t]; !seen {
dict[t] = struct{}{}
result = append(result, t)
}
}
return result
}
// simpleLog calculates a simplified log2 of an int.
// We use this to estimate sizes of various dictionaries,
// since the number of unique words in a corpus normally
// grows logarithmically in the number of words.
func simpleLog(corpusSize int) int {
return bits.Len(uint(corpusSize))
}
// Reduce collects a slice of elements E into a result of type R.
// This operation is also known as "fold" in other frameworks.
//
// It is a simplified form of seq.Reduce found in the core package, and works with
// standard seq.FuncCollect functions like seq.MakeString, seq.MakeBytes, seq.Count,
// seq.MakeSet, fnmath.Min, fnmath.Max, fnmath.Sum etc.
//
// The first argument "result" is the initial state. The second is the collector
// function. The signature of the collector works like standard append(). The last
// argument is the slice to collect values from.
//
// # Example:
//
// Summing the elements in and integer slice could look like:
//
// vals := []int{1, 2, 3}
// sum := slice.Reduce(func(res, e int) int {
// return res + e
// }, 0, vals)
//
// // sum is now: 6
func Reduce[E any, R any](collector func(R, E) R, result R, from []E) R {
for _, e := range from {
result = collector(result, e)
}
return result
}