-
Notifications
You must be signed in to change notification settings - Fork 6
/
last.go
90 lines (84 loc) · 2.7 KB
/
last.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
package go2linq
import (
"iter"
"github.com/solsw/errorhelper"
"github.com/solsw/generichelper"
)
// [Last] returns the last element of a sequence.
//
// [Last]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.last
func Last[Source any](source iter.Seq[Source]) (Source, error) {
if source == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSource)
}
empty := true
var res Source
for s := range source {
empty = false
res = s
}
if empty {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrEmptySource)
}
return res, nil
}
// [LastPred] returns the last element of a sequence that satisfies a specified condition.
//
// [LastPred]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.last
func LastPred[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 res Source
for s := range source {
empty = false
if predicate(s) {
found = true
res = s
}
}
if empty {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrEmptySource)
}
if !found {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNoMatch)
}
return res, nil
}
// [LastOrDefault] returns the last element of a sequence or a [zero value] if the sequence contains no elements.
//
// [LastOrDefault]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.lastordefault
// [zero value]: https://go.dev/ref/spec#The_zero_value
func LastOrDefault[Source any](source iter.Seq[Source]) (Source, error) {
if source == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSource)
}
res, err := Last(source)
if err != nil {
return generichelper.ZeroValue[Source](), nil
}
return res, nil
}
// [LastOrDefaultPred] returns the last element of a sequence that satisfies a condition
// or a [zero value] if no such element is found.
//
// [LastOrDefaultPred]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.lastordefault
// [zero value]: https://go.dev/ref/spec#The_zero_value
func LastOrDefaultPred[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)
}
res, err := LastPred(source, predicate)
if err != nil {
return generichelper.ZeroValue[Source](), nil
}
return res, nil
}