-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdropwhile.go
69 lines (53 loc) · 1.32 KB
/
dropwhile.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
package ranges
// dropWhileResult implements DropWhile
type dropWhileResult[T any] struct {
r InputRange[T]
cb func(element T) bool
}
func (dwr *dropWhileResult[T]) prime() {
if dwr.cb != nil {
for !dwr.r.Empty() {
front := dwr.r.Front()
if dwr.cb(front) {
dwr.r.PopFront()
} else {
dwr.cb = nil
break
}
}
}
}
func (dwr *dropWhileResult[T]) Empty() bool {
dwr.prime()
return dwr.r.Empty()
}
func (dwr *dropWhileResult[T]) Front() T {
dwr.prime()
return dwr.r.Front()
}
func (dwr *dropWhileResult[T]) PopFront() {
dwr.prime()
dwr.r.PopFront()
}
// dropWhileForwardResult implements DropWhileF
type dropWhileForwardResult[T any] struct {
dropWhileResult[T]
}
func (dwfr *dropWhileForwardResult[T]) Save() ForwardRange[T] {
dwfr.prime()
return &dropWhileForwardResult[T]{dropWhileResult[T]{dwfr.r.(ForwardRange[T]).Save(), dwfr.cb}}
}
// DropWhile advances a range while cb(element) returns `true`
func DropWhile[T any](r InputRange[T], cb func(element T) bool) InputRange[T] {
if cb == nil {
panic("cb is nil")
}
return &dropWhileResult[T]{r, cb}
}
// DropWhileF is DropWhile where the range can be saved.
func DropWhileF[T any](r ForwardRange[T], cb func(element T) bool) ForwardRange[T] {
if cb == nil {
panic("cb is nil")
}
return &dropWhileForwardResult[T]{dropWhileResult[T]{r, cb}}
}