-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathasync_adjacent_filter_sequence.go
More file actions
84 lines (69 loc) · 1.88 KB
/
Copy pathasync_adjacent_filter_sequence.go
File metadata and controls
84 lines (69 loc) · 1.88 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
73
74
75
76
77
78
79
80
81
82
83
84
package co
import (
syncx "go.tempura.ink/co/internal/syncx"
)
type AsyncAdjacentFilterSequence[R any] struct {
*asyncSequence[R]
previousIterator Iterator[R]
predictorFn func(R, R) bool
}
func NewAsyncAdjacentFilterSequence[R any](it AsyncSequenceable[R], fn func(R, R) bool) *AsyncAdjacentFilterSequence[R] {
a := &AsyncAdjacentFilterSequence[R]{
previousIterator: it.iterator(),
predictorFn: fn,
}
a.asyncSequence = NewAsyncSequence[R](a)
return a
}
func (c *AsyncAdjacentFilterSequence[R]) SetPredicator(fn func(R, R) bool) *AsyncAdjacentFilterSequence[R] {
c.predictorFn = fn
return c
}
func (c *AsyncAdjacentFilterSequence[R]) iterator() Iterator[R] {
it := &asyncAdjacentFilterSequenceIterator[R]{
AsyncAdjacentFilterSequence: c,
}
it.asyncSequenceIterator = NewAsyncSequenceIterator[R](it)
return it
}
type asyncAdjacentFilterSequenceIterator[R any] struct {
*asyncSequenceIterator[R]
*AsyncAdjacentFilterSequence[R]
previousData Optional[R]
}
func (it *asyncAdjacentFilterSequenceIterator[R]) preflight() {
if it.previousData.valid {
return
}
for op := it.previousIterator.next(); op.valid; op = it.previousIterator.next() {
it.previousData = *op
break
}
}
func (it *asyncAdjacentFilterSequenceIterator[R]) next() *Optional[R] {
it.preflight()
if !it.previousData.valid {
return NewOptionalEmpty[R]()
}
previousValue := it.previousData.data
for op := it.previousIterator.next(); op.valid; op = it.previousIterator.next() {
it.previousData = *op
match, err := syncx.SafeFn(func() bool {
return it.predictorFn(previousValue, op.data)
})
if err != nil {
it.handleError(err)
if it.errorMode.shouldSkip() {
continue
}
if it.errorMode.shouldStop() {
return NewOptionalEmpty[R]()
}
}
if match {
return OptionalOf(previousValue)
}
}
it.previousData = *NewOptionalEmpty[R]()
return OptionalOf(previousValue)
}