-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflat_map.go
More file actions
72 lines (63 loc) · 1.9 KB
/
flat_map.go
File metadata and controls
72 lines (63 loc) · 1.9 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
65
66
67
68
69
70
71
72
package enumerators
import "errors"
// flatMapEnumerator applies a function that returns an enumerator for each element,
// then flattens the results into a single sequence.
type flatMapEnumerator[T any, U any] struct {
base Enumerator[T]
mapper func(T) Enumerator[U]
current Enumerator[U]
err error
}
// MoveNext advances through the flattened sequence of enumerators.
// Returns true if more elements are available, false otherwise.
func (e *flatMapEnumerator[T, U]) MoveNext() bool {
// If we have a current enumerator, try to advance it
for {
if e.current != nil {
if e.current.MoveNext() {
return true
}
e.current.Dispose()
e.current = nil
}
// Move to the next item in the base enumerator
if !e.base.MoveNext() {
e.err = e.base.Err()
return false
}
// Get the next enumerator from the mapper
item, err := e.base.Current()
if err != nil {
return false
}
e.current = e.mapper(item)
}
}
// Current returns the current element from the active sub-enumerator.
func (e *flatMapEnumerator[T, U]) Current() (U, error) {
if e.current == nil {
var zero U
return zero, errors.New("no current item")
}
return e.current.Current()
}
// Err returns any error encountered during enumeration.
func (e *flatMapEnumerator[T, U]) Err() error {
return e.err
}
// Dispose cleans up resources by disposing both the base and current enumerators.
func (e *flatMapEnumerator[T, U]) Dispose() {
e.base.Dispose()
if e.current != nil {
e.current.Dispose()
}
}
// FlatMap creates an enumerator that applies a function to each element and flattens the results.
// The mapper function receives an element of type T and returns an enumerator of type U.
// All elements from each returned enumerator are yielded in sequence.
func FlatMap[T any, U any](parent Enumerator[T], mapper func(T) Enumerator[U]) Enumerator[U] {
return &flatMapEnumerator[T, U]{
base: parent,
mapper: mapper,
}
}