-
Notifications
You must be signed in to change notification settings - Fork 6
/
distinctby.go
123 lines (113 loc) · 4.28 KB
/
distinctby.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package go2linq
import (
"github.com/solsw/collate"
"github.com/solsw/errorhelper"
)
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.distinctby
// [DistinctBy] returns distinct elements from a sequence according to a specified key selector function
// and using [collate.DeepEqualer] to compare keys.
//
// [DistinctBy]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.distinctby
func DistinctBy[Source, Key any](source Enumerable[Source], keySelector func(Source) Key) (Enumerable[Source], error) {
if source == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
return DistinctByEq(source, keySelector, nil)
}
// DistinctByMust is like [DistinctBy] but panics in case of error.
func DistinctByMust[Source, Key any](source Enumerable[Source], keySelector func(Source) Key) Enumerable[Source] {
return errorhelper.Must(DistinctBy(source, keySelector))
}
func factoryDistinctByEq[Source, Key any](source Enumerable[Source], keySelector func(Source) Key, equaler collate.Equaler[Key]) func() Enumerator[Source] {
return func() Enumerator[Source] {
enr := source.GetEnumerator()
var c Source
seen := make([]Key, 0)
return enrFunc[Source]{
mvNxt: func() bool {
for enr.MoveNext() {
c = enr.Current()
k := keySelector(c)
if !elInElelEq(k, seen, equaler) {
seen = append(seen, k)
return true
}
}
return false
},
crrnt: func() Source { return c },
rst: func() { seen = make([]Key, 0); enr.Reset() },
}
}
}
// [DistinctByEq] returns distinct elements from a sequence according to a specified key selector function
// and using a specified equaler to compare keys. If 'equaler' is nil, [collate.DeepEqualer] is used.
//
// [DistinctByEq]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.distinctby
func DistinctByEq[Source, Key any](source Enumerable[Source],
keySelector func(Source) Key, equaler collate.Equaler[Key]) (Enumerable[Source], error) {
if source == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
if equaler == nil {
equaler = collate.DeepEqualer[Key]{}
}
return OnFactory(factoryDistinctByEq(source, keySelector, equaler)), nil
}
// DistinctByEqMust is like [DistinctByEq] but panics in case of error.
func DistinctByEqMust[Source, Key any](source Enumerable[Source],
keySelector func(Source) Key, equaler collate.Equaler[Key]) Enumerable[Source] {
return errorhelper.Must(DistinctByEq(source, keySelector, equaler))
}
func factoryDistinctByCmp[Source, Key any](source Enumerable[Source],
keySelector func(Source) Key, comparer collate.Comparer[Key]) func() Enumerator[Source] {
return func() Enumerator[Source] {
enr := source.GetEnumerator()
var c Source
seen := make([]Key, 0)
return enrFunc[Source]{
mvNxt: func() bool {
for enr.MoveNext() {
c = enr.Current()
k := keySelector(c)
i := elIdxInElelCmp(k, seen, comparer)
if i == len(seen) || comparer.Compare(k, seen[i]) != 0 {
elIntoElelAtIdx(k, &seen, i)
return true
}
}
return false
},
crrnt: func() Source { return c },
rst: func() { seen = make([]Key, 0); enr.Reset() },
}
}
}
// [DistinctByCmp] returns distinct elements from a sequence according to a specified key selector function
// and using a specified comparer to compare keys. (See [DistinctCmp].)
//
// [DistinctByCmp]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.distinctby
func DistinctByCmp[Source, Key any](source Enumerable[Source],
keySelector func(Source) Key, comparer collate.Comparer[Key]) (Enumerable[Source], error) {
if source == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if keySelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
if comparer == nil {
return nil, errorhelper.CallerError(ErrNilComparer)
}
return OnFactory(factoryDistinctByCmp(source, keySelector, comparer)), nil
}
// DistinctByCmpMust is like [DistinctByCmp] but panics in case of error.
func DistinctByCmpMust[Source, Key any](source Enumerable[Source],
keySelector func(Source) Key, comparer collate.Comparer[Key]) Enumerable[Source] {
return errorhelper.Must(DistinctByCmp(source, keySelector, comparer))
}