forked from ahmetb/go-linq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupby.go
81 lines (68 loc) · 2 KB
/
groupby.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package linq
// Group is a type that is used to store the result of GroupBy method.
type Group struct {
Key interface{}
Group []interface{}
}
// GroupBy method groups the elements of a collection according to a specified
// key selector function and projects the elements for each group by using a
// specified function.
func (q Query) GroupBy(keySelector func(interface{}) interface{},
elementSelector func(interface{}) interface{}) Query {
return Query{
func() Iterator {
next := q.Iterate()
set := make(map[interface{}][]interface{})
for item, ok := next(); ok; item, ok = next() {
key := keySelector(item)
set[key] = append(set[key], elementSelector(item))
}
len := len(set)
idx := 0
groups := make([]Group, len)
for k, v := range set {
groups[idx] = Group{k, v}
idx++
}
index := 0
return func() (item interface{}, ok bool) {
ok = index < len
if ok {
item = groups[index]
index++
}
return
}
},
}
}
// GroupByT is the typed version of GroupBy.
//
// - keySelectorFn is of type "func(TSource) TKey"
// - elementSelectorFn is of type "func(TSource) TElement"
//
// NOTE: GroupBy has better performance than GroupByT.
func (q Query) GroupByT(keySelectorFn interface{},
elementSelectorFn interface{}) Query {
keySelectorGenericFunc, err := newGenericFunc(
"GroupByT", "keySelectorFn", keySelectorFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(genericType))),
)
if err != nil {
panic(err)
}
keySelectorFunc := func(item interface{}) interface{} {
return keySelectorGenericFunc.Call(item)
}
elementSelectorGenericFunc, err := newGenericFunc(
"GroupByT", "elementSelectorFn", elementSelectorFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(genericType))),
)
if err != nil {
panic(err)
}
elementSelectorFunc := func(item interface{}) interface{} {
return elementSelectorGenericFunc.Call(item)
}
return q.GroupBy(keySelectorFunc, elementSelectorFunc)
}