-
Notifications
You must be signed in to change notification settings - Fork 6
/
last.go
126 lines (116 loc) · 4.22 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
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
124
125
126
package go2linq
import (
"github.com/solsw/errorhelper"
"github.com/solsw/generichelper"
)
// Reimplementing LINQ to Objects: Part 11 - First/Single/Last and the …OrDefault versions
// https://codeblog.jonskeet.uk/2010/12/29/reimplementing-linq-to-objects-part-11-first-single-last-and-the-ordefault-versions/
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.last
// https://learn.microsoft.com/dotnet/api/system.linq.enumerable.lastordefault
// [Last] returns the last element of a sequence.
//
// [Last]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.last
func Last[Source any](source Enumerable[Source]) (Source, error) {
if source == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSource)
}
if counter, cok := source.(Counter); cok {
len := counter.Count()
if len == 0 {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrEmptySource)
}
if itemer, iok := source.(Itemer[Source]); iok {
return itemer.Item(len - 1), nil
}
}
enr := source.GetEnumerator()
if !enr.MoveNext() {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrEmptySource)
}
r := enr.Current()
for enr.MoveNext() {
r = enr.Current()
}
return r, nil
}
// LastMust is like [Last] but panics in case of error.
func LastMust[Source any](source Enumerable[Source]) Source {
return errorhelper.Must(Last(source))
}
// [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 Enumerable[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)
}
enr := source.GetEnumerator()
if !enr.MoveNext() {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrEmptySource)
}
found := false
var r Source
c := enr.Current()
if predicate(c) {
found = true
r = c
}
for enr.MoveNext() {
c = enr.Current()
if predicate(c) {
found = true
r = c
}
}
if !found {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNoMatch)
}
return r, nil
}
// LastPredMust is like [LastPred] but panics in case of error.
func LastPredMust[Source any](source Enumerable[Source], predicate func(Source) bool) Source {
return errorhelper.Must(LastPred(source, predicate))
}
// [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 Enumerable[Source]) (Source, error) {
if source == nil {
return generichelper.ZeroValue[Source](), errorhelper.CallerError(ErrNilSource)
}
r, err := Last(source)
if err != nil {
return generichelper.ZeroValue[Source](), nil
}
return r, nil
}
// LastOrDefaultMust is like [LastOrDefault] but panics in case of error.
func LastOrDefaultMust[Source any](source Enumerable[Source]) Source {
return errorhelper.Must(LastOrDefault(source))
}
// [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 Enumerable[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 := LastPred(source, predicate)
if err != nil {
return generichelper.ZeroValue[Source](), nil
}
return r, nil
}
// LastOrDefaultPredMust is like [LastOrDefaultPred] but panics in case of error.
func LastOrDefaultPredMust[Source any](source Enumerable[Source], predicate func(Source) bool) Source {
return errorhelper.Must(LastOrDefaultPred(source, predicate))
}