-
Notifications
You must be signed in to change notification settings - Fork 6
/
tolookup.go
91 lines (85 loc) · 3.22 KB
/
tolookup.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
82
83
84
85
86
87
88
89
90
91
package go2linq
import (
"iter"
"github.com/solsw/errorhelper"
"github.com/solsw/generichelper"
)
// [ToLookup] creates a [Lookup] from a sequence according to a specified key selector function.
// [generichelper.DeepEqual] is used to compare keys. 'source' is enumerated immediately.
//
// [ToLookup]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.tolookup
func ToLookup[Source, Key any](source iter.Seq[Source], keySelector func(Source) Key) (*Lookup[Key, Source], error) {
if source == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
r, err := ToLookupSelEq(source, keySelector, Identity[Source], generichelper.DeepEqual[Key])
if err != nil {
return nil, errorhelper.CallerError(err)
}
return r, nil
}
// [ToLookupEq] creates a [Lookup] from a sequence according to a specified key selector function and a key equaler.
// 'source' is enumerated immediately.
//
// [ToLookupEq]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.tolookup
func ToLookupEq[Source, Key any](source iter.Seq[Source],
keySelector func(Source) Key, equal func(Key, Key) bool) (*Lookup[Key, Source], error) {
if source == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
if equal == nil {
return nil, errorhelper.CallerError(ErrNilEqual)
}
r, err := ToLookupSelEq(source, keySelector, Identity[Source], equal)
if err != nil {
return nil, errorhelper.CallerError(err)
}
return r, nil
}
// [ToLookupSel] creates a [Lookup] from a according to specified key selector and element selector functions.
// 'source' is enumerated immediately.
//
// [ToLookupSel]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.tolookup
func ToLookupSel[Source, Key, Element any](source iter.Seq[Source],
keySelector func(Source) Key, elementSelector func(Source) Element) (*Lookup[Key, Element], error) {
if source == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil || elementSelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
r, err := ToLookupSelEq(source, keySelector, elementSelector, generichelper.DeepEqual[Key])
if err != nil {
return nil, errorhelper.CallerError(err)
}
return r, nil
}
// [ToLookupSelEq] creates a [Lookup] from a sequence according to
// a specified key selector function, an element selector function and a key equaler.
// 'source' is enumerated immediately.
//
// [ToLookupSelEq]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.tolookup
func ToLookupSelEq[Source, Key, Element any](source iter.Seq[Source],
keySelector func(Source) Key, elementSelector func(Source) Element, equal func(Key, Key) bool) (*Lookup[Key, Element], error) {
if source == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil || elementSelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
if equal == nil {
return nil, errorhelper.CallerError(ErrNilEqual)
}
lk := &Lookup[Key, Element]{groupings: []Grouping[Key, Element]{}, KeyEqual: equal}
for s := range source {
k := keySelector(s)
lk.Add(k, elementSelector(s))
}
return lk, nil
}