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
|
package slices
import (
"fmt"
)
func createNotUniqueKeyError[T comparable](key T) error {
return fmt.Errorf("duplicated key \"%v\" found. keys are supposed to be unique", key)
}
// GroupBy groups slice entities into map by key provided via keyGetter.
func GroupBy[S ~[]T, T any, K comparable](s S, keyGetter func(T) K) map[K][]T {
res := map[K][]T{}
for _, entity := range s {
key := keyGetter(entity)
res[key] = append(res[key], entity)
}
return res
}
// GroupByUniqueKey groups slice entities into map by key provided via keyGetter with assumption that each key is unique.
//
// Returns an error in case of key ununiqueness.
func GroupByUniqueKey[S ~[]T, T any, K comparable](s S, keyGetter func(T) K) (map[K]T, error) {
res := map[K]T{}
for _, entity := range s {
key := keyGetter(entity)
_, duplicated := res[key]
if duplicated {
return res, createNotUniqueKeyError(key)
}
res[key] = entity
}
return res, nil
}
// IndexedEntity stores an entity of original slice with its initial index in that slice
type IndexedEntity[T any] struct {
Value T
Index int
}
// GroupByWithIndex groups slice entities into map by key provided via keyGetter.
// Each entity of underlying result slice contains the value itself and its index in the original slice
// (See IndexedEntity).
func GroupByWithIndex[S ~[]T, T any, K comparable](s S, keyGetter func(T) K) map[K][]IndexedEntity[T] {
res := map[K][]IndexedEntity[T]{}
for i, entity := range s {
key := keyGetter(entity)
res[key] = append(res[key], IndexedEntity[T]{
Value: entity,
Index: i,
})
}
return res
}
// GroupByUniqueKeyWithIndex groups slice entities into map by key provided via keyGetter with assumption that
// each key is unique.
// Each result entity contains the value itself and its index in the original slice
// (See IndexedEntity).
//
// Returns an error in case of key ununiqueness.
func GroupByUniqueKeyWithIndex[S ~[]T, T any, K comparable](s S, keyGetter func(T) K) (map[K]IndexedEntity[T], error) {
res := map[K]IndexedEntity[T]{}
for i, entity := range s {
key := keyGetter(entity)
_, duplicated := res[key]
if duplicated {
return res, createNotUniqueKeyError(key)
}
res[key] = IndexedEntity[T]{
Value: entity,
Index: i,
}
}
return res, nil
}
|