-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsingle.go
101 lines (95 loc) · 3.26 KB
/
single.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
package go2linq
import (
"errors"
"iter"
"github.com/solsw/errorhelper"
"github.com/solsw/generichelper"
)
// [Single] returns the only element of a sequence and returns an error if there is not exactly one element in the sequence.
//
// [Single]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.single
func Single[Source any](source iter.Seq[Source]) (Source, error) {
if source == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSource)
}
next, stop := iter.Pull(source)
defer stop()
s, ok := next()
if !ok {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrEmptySource)
}
_, ok = next()
if ok {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrMultipleElements)
}
return s, nil
}
// [SinglePred] returns the only element of a sequence that satisfies a specified condition.
//
// [SinglePred]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.single
func SinglePred[Source any](source iter.Seq[Source], predicate func(Source) bool) (Source, error) {
if source == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSource)
}
if predicate == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilPredicate)
}
empty := true
found := false
var r Source
for s := range source {
empty = false
if predicate(s) {
if found {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrMultipleMatch)
}
found = true
r = s
}
}
if empty {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrEmptySource)
}
if !found {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNoMatch)
}
return r, nil
}
// [SingleOrDefault] returns the only element of a sequence or a [zero value] if the sequence is empty.
//
// [SingleOrDefault]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.singleordefault
// [zero value]: https://go.dev/ref/spec#The_zero_value
func SingleOrDefault[Source any](source iter.Seq[Source]) (Source, error) {
if source == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSource)
}
r, err := Single(source)
if err != nil {
if errors.Is(err, ErrMultipleElements) {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrMultipleElements)
}
return generichelper.ZeroValue[Source](), nil
}
return r, nil
}
// [SingleOrDefaultPred] returns the only element of a sequence that satisfies a specified condition
// or a [zero value] if no such element exists.
//
// [SingleOrDefaultPred]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.singleordefault
// [zero value]: https://go.dev/ref/spec#The_zero_value
func SingleOrDefaultPred[Source any](source iter.Seq[Source], predicate func(Source) bool) (Source, error) {
if source == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSource)
}
if predicate == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilPredicate)
}
r, err := SinglePred(source, predicate)
if err != nil {
if errors.Is(err, ErrMultipleMatch) {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrMultipleMatch)
}
return generichelper.ZeroValue[Source](), nil
}
return r, nil
}