-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathselectmany.go
107 lines (101 loc) · 3.21 KB
/
selectmany.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
package go2linq
import (
"iter"
"github.com/solsw/errorhelper"
)
// [SelectMany] projects each element of a sequence to another sequence
// and flattens the resulting sequences into one sequence.
//
// [SelectMany]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.selectmany
func SelectMany[Source, Result any](source iter.Seq[Source], selector func(Source) iter.Seq[Result]) (iter.Seq[Result], error) {
if source == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if selector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
return func(yield func(Result) bool) {
for s := range source {
for r := range selector(s) {
if !yield(r) {
return
}
}
}
},
nil
}
// [SelectManyIdx] projects each element of a sequence and its index to another sequence
// and flattens the resulting sequences into one sequence.
//
// [SelectManyIdx]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.selectmany
func SelectManyIdx[Source, Result any](source iter.Seq[Source], selector func(Source, int) iter.Seq[Result]) (iter.Seq[Result], error) {
if source == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if selector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
return func(yield func(Result) bool) {
i := 0
for s := range source {
for r := range selector(s, i) {
if !yield(r) {
return
}
}
i++
}
},
nil
}
// [SelectManyColl] projects each element of a sequence to another sequence,
// flattens the resulting sequences into one sequence
// and invokes a result selector function on each element therein.
//
// [SelectManyColl]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.selectmany
func SelectManyColl[Source, Collection, Result any](source iter.Seq[Source],
collectionSelector func(Source) iter.Seq[Collection], resultSelector func(Source, Collection) Result) (iter.Seq[Result], error) {
if source == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if collectionSelector == nil || resultSelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
return func(yield func(Result) bool) {
for s := range source {
for c := range collectionSelector(s) {
if !yield(resultSelector(s, c)) {
return
}
}
}
},
nil
}
// [SelectManyCollIdx] projects each element of a sequence and its index to another sequence,
// flattens the resulting sequences into one sequence
// and invokes a result selector function on each element therein.
//
// [SelectManyCollIdx]: https://learn.microsoft.com/dotnet/api/system.linq.enumerable.selectmany
func SelectManyCollIdx[Source, Collection, Result any](source iter.Seq[Source],
collectionSelector func(Source, int) iter.Seq[Collection], resultSelector func(Source, Collection) Result) (iter.Seq[Result], error) {
if source == nil {
return nil, errorhelper.CallerError(ErrNilSource)
}
if collectionSelector == nil || resultSelector == nil {
return nil, errorhelper.CallerError(ErrNilSelector)
}
return func(yield func(Result) bool) {
i := 0
for s := range source {
for c := range collectionSelector(s, i) {
if !yield(resultSelector(s, c)) {
return
}
}
i++
}
},
nil
}