-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtakewhile.go
57 lines (44 loc) · 1.15 KB
/
takewhile.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
package ranges
type takeWhileResult[T any] struct {
r InputRange[T]
cb func(element T) bool
isPrimed bool
}
func (u *takeWhileResult[T]) prime() {
if !u.isPrimed {
if !u.r.Empty() {
front := u.r.Front()
if !u.cb(front) {
u.r = Null[T]()
}
}
u.isPrimed = true
}
}
func (u *takeWhileResult[T]) Empty() bool {
u.prime()
return u.r.Empty()
}
func (u *takeWhileResult[T]) Front() T {
u.prime()
return u.r.Front()
}
func (u *takeWhileResult[T]) PopFront() {
u.prime()
u.r.PopFront()
u.isPrimed = false
}
type takeWhileForwardResult[T any] struct {
takeWhileResult[T]
}
func (u *takeWhileForwardResult[T]) Save() ForwardRange[T] {
return &takeWhileForwardResult[T]{takeWhileResult[T]{u.r.(ForwardRange[T]).Save(), u.cb, u.isPrimed}}
}
// TakeWhile advances a range while `cb` returns `true`
func TakeWhile[T any](r InputRange[T], cb func(element T) bool) InputRange[T] {
return &takeWhileResult[T]{r, cb, false}
}
// TakeWhileF is `TakeWhile` producing a `ForwardRange`
func TakeWhileF[T any](r ForwardRange[T], cb func(element T) bool) ForwardRange[T] {
return &takeWhileForwardResult[T]{takeWhileResult[T]{r, cb, false}}
}