-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_map.go
More file actions
64 lines (53 loc) · 1.59 KB
/
Copy pathfilter_map.go
File metadata and controls
64 lines (53 loc) · 1.59 KB
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
package enumerators
// filterMapper applies a transformation and filtering function to each element from the base enumerator.
type filterMapper[TIn any, TOut any] struct {
base Enumerator[TIn]
apply func(TIn) (TOut, bool, error)
current TOut
err error
}
// MoveNext advances to the next element that is both transformed and accepted by the filter.
func (e *filterMapper[TIn, TOut]) MoveNext() bool {
for {
if !e.base.MoveNext() {
return false
}
item, err := e.base.Current()
if err != nil {
e.err = err
return false
}
u, ok, err := e.apply(item)
if err != nil {
e.err = err
return false
}
if !ok {
continue
}
e.err = err
e.current = u
return true
}
}
// Current returns the transformed current element and any error encountered.
func (e *filterMapper[TIn, TOut]) Current() (TOut, error) {
return e.current, e.err
}
// Err returns any error encountered during enumeration or transformation.
func (e *filterMapper[TIn, TOut]) Err() error {
return e.err
}
// Dispose cleans up resources by disposing the underlying enumerator.
func (e *filterMapper[TIn, TOut]) Dispose() {
e.base.Dispose()
}
// FilterMap creates an enumerator that applies both transformation and filtering in a single pass.
// The apply function receives an element and returns (transformedValue, shouldInclude, error).
// Only elements where shouldInclude is true are yielded after transformation.
func FilterMap[TIn any, TOut any](enumerator Enumerator[TIn], apply func(TIn) (TOut, bool, error)) Enumerator[TOut] {
return &filterMapper[TIn, TOut]{
base: enumerator,
apply: apply,
}
}